Skip to content

Commit

Permalink
update (test cases): the issue is fixed due to missing an id
Browse files Browse the repository at this point in the history
  • Loading branch information
dependentmadani committed Sep 28, 2024
1 parent c09ad3a commit 9872d5e
Showing 1 changed file with 20 additions and 14 deletions.
34 changes: 20 additions & 14 deletions src/repository/storage/postgres/orm/sequelize/noteRelations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type Orm from '@repository/storage/postgres/orm/sequelize/index.js';
import type { NoteInternalId } from '@domain/entities/note.js';
import type { Note } from '@domain/entities/note.js';
import { Model, DataTypes } from 'sequelize';
import { notEmpty } from '@infrastructure/utils/empty.js';
import { isEmpty, notEmpty } from '@infrastructure/utils/empty.js';

/**
* Class representing a note relations in database
Expand Down Expand Up @@ -217,21 +217,27 @@ export default class NoteRelationsSequelizeStorage {
* @param noteId - the ID of the note.
*/
public async getAllNoteParentsIds(noteId: NoteInternalId): Promise<NoteInternalId[]> {
if (!this.noteModel) {
throw new Error('NoteRelationStorage: Note model is not defined');
}

const parentNotes: NoteInternalId[] = [];
let currentNoteId: number | null = noteId;

// get all note ids via a singe sql query
const noteRelation: NoteRelationsModel[] | null = await this.model.findAll({
where: { noteId },
attributes: ['noteId'],
raw: true,
});

if (notEmpty(noteRelation)) {
parentNotes.push(...noteRelation.map(relation => relation.noteId));
// get all note ids via a singe sql query instead of many
while (currentNoteId !== null) {
const noteRelation: NoteRelationsModel | null = await this.model.findOne({
where: {
noteId: currentNoteId,
},
});

if (notEmpty(noteRelation)) {
parentNotes.push(noteRelation.noteId);
}

if (isEmpty(noteRelation)) {
parentNotes.push(currentNoteId);
break;
} else {
currentNoteId = noteRelation.parentId ?? null;
}
}

parentNotes.reverse();
Expand Down

0 comments on commit 9872d5e

Please sign in to comment.