Skip to content

Commit

Permalink
Merge pull request #80 from AbdulrhmanGoni/refactor/all-users-data-in…
Browse files Browse the repository at this point in the history
…-one-collection

Merge "datasets" & "recent-activities" collections into "users" collections
  • Loading branch information
AbdulrhmanGoni authored Nov 23, 2024
2 parents 90d6011 + 91436fc commit 76c2277
Show file tree
Hide file tree
Showing 39 changed files with 227 additions and 226 deletions.
54 changes: 0 additions & 54 deletions src/models/RecentActivitiesModel.ts

This file was deleted.

39 changes: 36 additions & 3 deletions src/models/DatasetsModel.ts → src/models/UserModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,22 @@ const DatasetSchema = new Schema<DatasetDocument>(
}
);

const DatasetsSchema = new Schema(
const ActivitySchema = {
dataset: {
type: Schema.Types.Mixed,
required: true,
},
activity: {
type: String,
required: true,
},
activityDate: {
type: Date,
required: true,
},
};

const UserSchema = new Schema(
{
_id: {
type: String,
Expand Down Expand Up @@ -81,13 +96,31 @@ const DatasetsSchema = new Schema(
_id: false,
},
},
datasetsActivities: {
type: [ActivitySchema],
default: [],
required: true,
},
instructionsActivities: {
type: [
{
...ActivitySchema,
instruction: {
type: Schema.Types.Mixed,
required: true,
},
},
],
default: [],
required: true,
},
},
{
timestamps: false,
versionKey: false,
}
);

const DatasetsModel = model("datasets", DatasetsSchema);
const UserModel = model("user", UserSchema);

export default DatasetsModel;
export default UserModel;
12 changes: 6 additions & 6 deletions src/services/activities/getRecentActivities_service.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import ServiceOperationResult from "../../utilities/ServiceOperationResult";
import RecentActivitiesModel from "../../models/RecentActivitiesModel";
import UserModel from "../../models/UserModel";
import type { ServiceOperationResultType } from "../../types/response";

export default async function getRecentActivities_service(
userId: string
): Promise<ServiceOperationResultType> {
const result = await RecentActivitiesModel.findOne(
const result = await UserModel.findOne(
{ _id: userId },
{ _id: 0 },
{ _id: 0, datasetsActivities: 1, instructionsActivities: 1 },
{ upsert: true }
);
return ServiceOperationResult.success(
result
? result
: {
datasetsActivities: [],
instructionsActivities: [],
}
datasetsActivities: [],
instructionsActivities: [],
}
);
}
4 changes: 2 additions & 2 deletions src/services/activities/registerActivity_service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import ServiceOperationResult from "../../utilities/ServiceOperationResult";
import RecentActivitiesModel from "../../models/RecentActivitiesModel";
import UserModel from "../../models/UserModel";
import type { Activity, ActivityResource } from "../../types/activities";
import type { ServiceOperationResultType } from "../../types/response";
import operationsResultsMessages from "../../constants/operationsResultsMessages";
Expand All @@ -19,7 +19,7 @@ export default async function registerActivity_service(
},
};

const result = await RecentActivitiesModel.updateOne(
const result = await UserModel.updateOne(
{ _id: userId },
updateQuery,
{ upsert: true }
Expand Down
16 changes: 9 additions & 7 deletions src/services/datasets/createDataset_service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import DatasetsModel from "../../models/DatasetsModel";
import UserModel from "../../models/UserModel";
import { DatasetDocument, type DatasetInput } from "../../types/datasets";
import type { ServiceOperationResultType } from "../../types/response";
import ServiceOperationResult from "../../utilities/ServiceOperationResult";
Expand All @@ -12,23 +12,25 @@ export default async function createDataset_service(
): Promise<ServiceOperationResultType> {
const newDataset = new DatasetDocument(dataset);

const userDatasets = await DatasetsModel.findById(
const userData = await UserModel.findById(
userId,
{},
{ upsert: true }
);

if (!userDatasets) {
await DatasetsModel.create({
if (!userData) {
await UserModel.create({
_id: userId,
datasets: [newDataset],
datasetsActivities: [],
instructionsActivities: [],
});
return successfulDatasetCreation(userId, newDataset);
}

if (userDatasets.datasets.length < maxDatasetsForUser) {
userDatasets.datasets.push(newDataset);
await userDatasets.save();
if (userData.datasets.length < maxDatasetsForUser) {
userData.datasets.push(newDataset);
await userData.save();
return successfulDatasetCreation(userId, newDataset);
} else {
return ServiceOperationResult.failure(
Expand Down
8 changes: 4 additions & 4 deletions src/services/datasets/datasetsOverview_service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import ServiceOperationResult from "../../utilities/ServiceOperationResult";
import DatasetModel from "../../models/DatasetsModel";
import DatasetModel from "../../models/UserModel";
import type { ServiceOperationResultType } from "../../types/response";

export default async function datasetsOverview_service(
Expand Down Expand Up @@ -35,8 +35,8 @@ export default async function datasetsOverview_service(
result
? result
: {
totalDatasets: 0,
addedDatasetsLastMonth: 0,
}
totalDatasets: 0,
addedDatasetsLastMonth: 0,
}
);
}
4 changes: 2 additions & 2 deletions src/services/datasets/deleteDataset_service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import operationsResultsMessages from "../../constants/operationsResultsMessages";
import DatasetsModel from "../../models/DatasetsModel";
import UserModel from "../../models/UserModel";
import type { Dataset } from "../../types/datasets";
import type { ServiceOperationResultType } from "../../types/response";
import createTransactionSession from "../../utilities/createTransactionSession";
Expand All @@ -13,7 +13,7 @@ export default async function deleteDataset_service(
): Promise<ServiceOperationResultType> {
const session = await createTransactionSession();

const result = await DatasetsModel.findByIdAndUpdate(
const result = await UserModel.findByIdAndUpdate(
{ _id: userId },
{ $pull: { datasets: { _id: datasetId } } },
{ projection: { datasets: 1 }, session }
Expand Down
2 changes: 1 addition & 1 deletion src/services/datasets/getDatasetById_service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import ServiceOperationResult from "../../utilities/ServiceOperationResult";
import DatasetModel from "../../models/DatasetsModel";
import DatasetModel from "../../models/UserModel";
import type { Dataset, DatasetDocument } from "../../types/datasets";
import type { ServiceOperationResultType } from "../../types/response";
import operationsResultsMessages from "../../constants/operationsResultsMessages";
Expand Down
4 changes: 2 additions & 2 deletions src/services/datasets/getDatasets_service.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import ServiceOperationResult from "../../utilities/ServiceOperationResult";
import DatasetsModel from "../../models/DatasetsModel";
import UserModel from "../../models/UserModel";
import type { ServiceOperationResultType } from "../../types/response";
import operationsResultsMessages from "../../constants/operationsResultsMessages";

export default async function getDatasets_service(
userId: string
): Promise<ServiceOperationResultType> {
const result = await DatasetsModel.findOne({ _id: userId });
const result = await UserModel.findOne({ _id: userId }, { datasets: true });

return ServiceOperationResult.success(
result?.datasets || [],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { type ClientSession } from "mongoose";
import ServiceOperationResult from "../../utilities/ServiceOperationResult";
import DatasetModel from "../../models/DatasetsModel";
import DatasetModel from "../../models/UserModel";
import type { Dataset } from "../../types/datasets";
import type { ServiceOperationResultType } from "../../types/response";
import operationsResultsMessages from "../../constants/operationsResultsMessages";
Expand Down
6 changes: 3 additions & 3 deletions src/services/datasets/setDatasetRepository_service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Types, type ClientSession } from "mongoose";
import DatasetsModel from "../../models/DatasetsModel";
import UserModel from "../../models/UserModel";
import type { Dataset, DatasetDocument } from "../../types/datasets";
import type { ServiceOperationResultType } from "../../types/response";
import ServiceOperationResult from "../../utilities/ServiceOperationResult";
Expand Down Expand Up @@ -39,7 +39,7 @@ export default async function setDatasetRepository_service({
};
}

const data = await DatasetsModel.findByIdAndUpdate(
const userData = await UserModel.findByIdAndUpdate(
{ _id: userId },
updateObject,
{
Expand All @@ -49,7 +49,7 @@ export default async function setDatasetRepository_service({
}
);

const dataset = data?.datasets.find((dataset) => dataset.id === datasetId);
const dataset = userData?.datasets.find((dataset) => dataset.id === datasetId);

if (dataset) {
return ServiceOperationResult.success(dataset);
Expand Down
2 changes: 1 addition & 1 deletion src/services/datasets/updateDataset_service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import operationsResultsMessages from "../../constants/operationsResultsMessages";
import DatasetModel from "../../models/DatasetsModel";
import DatasetModel from "../../models/UserModel";
import type { Dataset, UpdateDatasetInput } from "../../types/datasets";
import type { ServiceOperationResultType } from "../../types/response";
import ServiceOperationResult from "../../utilities/ServiceOperationResult";
Expand Down
4 changes: 2 additions & 2 deletions src/services/huggingface/createHuggingfaceAccount_service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { whoAmI, type WhoAmIUser } from "@huggingface/hub";
import DatasetsModel from "../../models/DatasetsModel";
import UserModel from "../../models/UserModel";
import ServiceOperationResult from "../../utilities/ServiceOperationResult";
import operationsResultsMessages from "../../constants/operationsResultsMessages";

Expand All @@ -17,7 +17,7 @@ export default async function createHuggingfaceAccount_service(
accessToken: credentials.hfAccessToken,
})) as WhoAmIUser;

const { modifiedCount } = await DatasetsModel.updateOne(
const { modifiedCount } = await UserModel.updateOne(
{ _id: userId },
{
$set: {
Expand Down
4 changes: 2 additions & 2 deletions src/services/huggingface/getHuggingfaceAccount_service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import DatasetsModel from "../../models/DatasetsModel";
import UserModel from "../../models/UserModel";
import ServiceOperationResult from "../../utilities/ServiceOperationResult";
import type { ServiceOperationResultType } from "../../types/response";
import type { UserHuggingfaceAccount } from "../../types/huggingface";
Expand All @@ -9,7 +9,7 @@ export default async function getHuggingfaceAccount_service(
this: HuggingfaceService,
userId: string
): Promise<ServiceOperationResultType<UserHuggingfaceAccount>> {
const userData = await DatasetsModel.findById(userId, {
const userData = await UserModel.findById(userId, {
huggingfaceAccount: 1,
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import DatasetsModel from "../../models/DatasetsModel";
import UserModel from "../../models/UserModel";
import ServiceOperationResult from "../../utilities/ServiceOperationResult";
import { refreshAccessToken } from "./huggingFaceOAuthTokenRequests";
import operationsResultsMessages from "../../constants/operationsResultsMessages";
Expand All @@ -10,7 +10,7 @@ export default async function refreshHuggingfaceAccessToken_service(
const refreshTokenResult = await refreshAccessToken(refreshToken);

if (refreshTokenResult.success) {
const updateResult = await DatasetsModel.findByIdAndUpdate(
const updateResult = await UserModel.findByIdAndUpdate(
{ _id: userId },
{
$set: {
Expand Down
7 changes: 4 additions & 3 deletions tests/e2e/activities/getRecentActivities.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, it } from "bun:test";
import { request } from "../..";
import RecentActivitiesModel from "../../../src/models/RecentActivitiesModel";
import UserModel from "../../../src/models/UserModel";
import { afterAll } from "bun:test";
import {
fakeRecentDatasetActivities,
Expand All @@ -23,10 +23,11 @@ describe(`GET /${path}`, () => {
});

it("Should return an object contains activities arrays", async () => {
await RecentActivitiesModel.create({
await UserModel.create({
_id: process.env.TESTING_USER_ID,
datasetsActivities: fakeRecentDatasetActivities,
instructionsActivities: fakeRecentInstructionActivities,
datasets: []
});

const { resBody, status } = await request.GET(path);
Expand Down Expand Up @@ -75,5 +76,5 @@ function datasetCheck(dataset: DatasetActivity["dataset"]) {
}

afterAll(async () => {
await RecentActivitiesModel.deleteMany();
await UserModel.deleteMany();
});
8 changes: 3 additions & 5 deletions tests/e2e/datasets/createDataset.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { afterAll, describe, expect, it } from "bun:test";
import { request } from "../..";
import { getRandomFakeDataset } from "../../fake-data/fakeDatasets";
import DatasetsModel from "../../../src/models/DatasetsModel";
import RecentActivitiesModel from "../../../src/models/RecentActivitiesModel";
import UserModel from "../../../src/models/UserModel";
import operationsResultsMessages from "../../../src/constants/operationsResultsMessages";
import maxDatasetsForUser from "../../../src/constants/maxDatasetsForUser";

Expand Down Expand Up @@ -33,7 +32,7 @@ describe(`POST /${path}`, () => {
testingDatasets.push(getRandomFakeDataset());
}

await DatasetsModel.updateOne(
await UserModel.updateOne(
{ _id: process.env.TESTING_USER_ID },
{ $set: { datasets: testingDatasets } }
);
Expand All @@ -57,6 +56,5 @@ describe(`POST /${path}`, () => {
});

afterAll(async () => {
await DatasetsModel.deleteMany();
await RecentActivitiesModel.deleteMany();
await UserModel.deleteMany();
});
Loading

0 comments on commit 76c2277

Please sign in to comment.