Skip to content

Commit

Permalink
feat(#391): Refactor mutation to use datasetId collection
Browse files Browse the repository at this point in the history
  • Loading branch information
tholulomo committed Jun 6, 2023
1 parent d725b30 commit 00a5067
Showing 1 changed file with 8 additions and 15 deletions.
23 changes: 8 additions & 15 deletions resfulservice/src/graphql/resolver/dataset/mutation.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const errorFormater = require('../../../utils/errorFormater');
const deleteFile = require('../../../utils/fileManager');
const Dataset = require('../../../models/dataset');
const User = require('../../../models/user');
const DatasetId = require('../../../models/datasetId');
const { datasetTransformer, filesetsTransform } = require('../../transformer');

const datasetMutation = {
Expand All @@ -12,25 +12,18 @@ const datasetMutation = {
return errorFormater('Unauthorized', 401);
}
try {
// TODO: Find a way to reduce latency here
const unusedDatasetId = await Dataset.findOne({ filesets: [] });
const { _id, displayName } = user;
const unusedDatasetId = await DatasetId.findOne({ user: _id, samples: [] });
if (unusedDatasetId?._id) {
req.logger.error('[createDatasetId]: Failed to create. User has unused existing dataset Id');
const err = { message: `An unused datasetId - ${unusedDatasetId?._id} exists` };
return errorFormater(err.message, 409);
}
const { userid } = user;
const datasetId = new Dataset({
userid,
dttm_created: Math.floor(Date.now() / 1000),
dttm_updated: Math.floor(Date.now() / 1000),
isPublic: false
});
const [savedDataset, userDetails] = await Promise.all([
datasetId.save(),
User.findOne({ userid }, { displayName: 1 })
]);
return datasetTransformer(savedDataset, userDetails);

const datasetId = new Dataset({ user: _id });
const savedDataset = await datasetId.save();

return datasetTransformer(savedDataset, { _id, displayName });
} catch (error) {
req.logger.error(`[createDatasetId]: ${error}`);
return errorFormater(error.message, 500);
Expand Down

0 comments on commit 00a5067

Please sign in to comment.