Skip to content

Commit db5152f

Browse files
committed
feat: duplicate filter now uses set, convert to array for findMany, create createSubjectDto array
1 parent da39af2 commit db5152f

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

apps/api/src/subjects/subjects.service.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,14 @@ export class SubjectsService {
4242
}
4343

4444
async createMany(data: CreateSubjectDto[]) {
45-
//filter out all duplicate ids that are planned to be created
45+
//filter out all duplicate ids that are planned to be created via a set
46+
const noDuplicatesSet = new Set(
47+
data.map((record) => {
48+
return record.id;
49+
})
50+
);
4651

47-
const seen = new Set();
48-
const noDuplicateSubjectsList = data.filter((record) => {
49-
if (!seen.has(record.id)) {
50-
seen.add(record.id);
51-
return true;
52-
}
53-
return false;
54-
});
55-
56-
const subjectIds = noDuplicateSubjectsList.map((record) => record.id);
52+
const subjectIds = Array.from(noDuplicatesSet);
5753

5854
//find the list of subject ids that already exist
5955
const existingSubjects = await this.subjectModel.findMany({
@@ -67,7 +63,13 @@ export class SubjectsService {
6763
const existingIds = new Set(existingSubjects.map((subj) => subj.id));
6864

6965
//Filter out records whose IDs already exist
70-
const subjectsToCreate = noDuplicateSubjectsList.filter((record) => !existingIds.has(record.id));
66+
const subjectsToCreateIds = subjectIds.filter((record) => !existingIds.has(record));
67+
68+
const subjectsToCreate: CreateSubjectDto[] = subjectsToCreateIds.map((record) => {
69+
return {
70+
id: record
71+
};
72+
});
7173

7274
//if there are none left to create do not follow through with the command
7375
if (subjectsToCreate.length < 1) {

0 commit comments

Comments
 (0)