-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert "Set a maximum number of datasets for each user"
- Loading branch information
1 parent
507689f
commit 8fd6b8f
Showing
3 changed files
with
30 additions
and
43 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,48 @@ | ||
import createTransactionSession from "../../utilities/createTransactionSession"; | ||
import DatasetsModel from "../../models/DatasetsModel"; | ||
import { DatasetDocument, type DatasetInput } from "../../types/datasets"; | ||
import type { ServiceOperationResultType } from "../../types/response"; | ||
import ServiceOperationResult from "../../utilities/ServiceOperationResult"; | ||
import activitiesService from "../activities"; | ||
import operationsResultsMessages from "../../constants/operationsResultsMessages"; | ||
import maxDatasetsForUser from "../../constants/maxDatasetsForUser"; | ||
|
||
export default async function createDataset_service( | ||
userId: string, | ||
dataset: DatasetInput | ||
): Promise<ServiceOperationResultType> { | ||
const session = await createTransactionSession(); | ||
|
||
const newDataset = new DatasetDocument(dataset); | ||
|
||
const userDatasets = await DatasetsModel.findById( | ||
userId, | ||
{}, | ||
{ upsert: true } | ||
); | ||
const datasetAdded = await DatasetsModel.updateOne( | ||
{ _id: userId }, | ||
{ $push: { datasets: newDataset } }, | ||
{ upsert: true, session } | ||
) | ||
.then(() => true) | ||
.catch(() => false); | ||
|
||
if (!userDatasets) { | ||
await DatasetsModel.create({ | ||
_id: userId, | ||
datasets: [newDataset], | ||
}); | ||
return successfulDatasetCreation(userId, newDataset); | ||
} | ||
if (datasetAdded) { | ||
await session.commitTransaction(); | ||
activitiesService.registerDatasetActivity( | ||
userId, | ||
{ | ||
_id: newDataset._id, | ||
name: newDataset.name, | ||
description: newDataset.description, | ||
}, | ||
newDataset.createdAt, | ||
"New Resource" | ||
); | ||
|
||
if (userDatasets.datasets.length < maxDatasetsForUser) { | ||
userDatasets.datasets.push(newDataset); | ||
await userDatasets.save(); | ||
return successfulDatasetCreation(userId, newDataset); | ||
} else { | ||
return ServiceOperationResult.failure( | ||
operationsResultsMessages.maxDatasetsReached | ||
return ServiceOperationResult.success( | ||
newDataset, | ||
operationsResultsMessages.successfulDatasetCreation(newDataset.name) | ||
); | ||
} | ||
} | ||
|
||
function successfulDatasetCreation( | ||
userId: string, | ||
newDataset: DatasetDocument | ||
) { | ||
activitiesService.registerDatasetActivity( | ||
userId, | ||
{ | ||
_id: newDataset._id, | ||
name: newDataset.name, | ||
description: newDataset.description, | ||
}, | ||
newDataset.createdAt, | ||
"New Resource" | ||
); | ||
|
||
return ServiceOperationResult.success( | ||
newDataset, | ||
operationsResultsMessages.successfulDatasetCreation(newDataset.name) | ||
await session.abortTransaction(); | ||
return ServiceOperationResult.failure( | ||
operationsResultsMessages.failedDatasetCreation(newDataset.name) | ||
); | ||
} |