Skip to content

Commit

Permalink
update (note parents): modify based on last review
Browse files Browse the repository at this point in the history
  • Loading branch information
dependentmadani committed Oct 16, 2024
1 parent ed49e60 commit 7bfe0eb
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 40 deletions.
8 changes: 2 additions & 6 deletions src/domain/service/note.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import type User from '@domain/entities/user.js';
import type { NoteList } from '@domain/entities/noteList.js';
import type NoteHistoryRepository from '@repository/noteHistory.repository.js';
import type { NoteHistoryMeta, NoteHistoryRecord, NoteHistoryPublic } from '@domain/entities/noteHistory.js';
import { definePublicNote, type NotePublic } from '@domain/entities/notePublic.js';

/**
* Note service
Expand Down Expand Up @@ -449,13 +448,10 @@ export default class NoteService {
* @param noteId - id of the note to get parent structure
* @returns - array of notes that are parent structure of the note
*/
public async getNoteParents(noteId: NoteInternalId): Promise<NotePublic[]> {
public async getNoteParents(noteId: NoteInternalId): Promise<Note[]> {
const noteIds: NoteInternalId[] = await this.noteRelationsRepository.getNoteParentsIds(noteId);
const noteParents = await this.noteRepository.getNotesByIds(noteIds);
const noteParentsPublic: NotePublic[] = noteParents.map((note) => {
return definePublicNote(note);
});

return noteParentsPublic;
return noteParents;
}
}
12 changes: 6 additions & 6 deletions src/presentation/http/router/note.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ describe('Note API', () => {
/** Create test user */
const user = await global.db.insertUser();

/** Create acces token for the user */
/** Create access token for the user */
const accessToken = global.auth(user.id);

/** Create test note - a parent note */
Expand Down Expand Up @@ -572,7 +572,7 @@ describe('Note API', () => {
/** Create test user */
const user = await global.db.insertUser();

/** Create acces token for the user */
/** Create access token for the user */
const accessToken = global.auth(user.id);

/** Create test note - a grand parent note */
Expand Down Expand Up @@ -632,14 +632,14 @@ describe('Note API', () => {
});
});

test('Returns one parent note when the user is not in the parent note\'s team but shares a note relation', async () => {
test('Returns one parent note when the user is not in the parent note\'s team but has note relation', async () => {
/** Create test user */
const user = await global.db.insertUser();

/** Create another user */
const anotherUser = await global.db.insertUser();

/** Create acces token for the user */
/** Create access token for the user */
const accessToken = global.auth(user.id);

/** Create test note - a parent note */
Expand Down Expand Up @@ -684,11 +684,11 @@ describe('Note API', () => {
});
});

test('Returns no note in case where there is no relation exist for the note with status 200', async () => {
test('Returns empty array in case where there is no relation exist for the note with status 200', async () => {
/** Create test user */
const user = await global.db.insertUser();

/** Create acces token for the user */
/** Create access token for the user */
const accessToken = global.auth(user.id);

/** Create test note - a child note */
Expand Down
6 changes: 5 additions & 1 deletion src/presentation/http/router/note.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,12 +181,16 @@ const NoteRouter: FastifyPluginCallback<NoteRouterOptions> = (fastify, opts, don

const noteParentStructure = await noteService.getNoteParents(noteId);

const noteParentsPublic = noteParentStructure.map((notes) => {
return definePublicNote(notes);
});

return reply.send({
note: notePublic,
parentNote: parentNote,
accessRights: { canEdit: canEdit },
tools: noteTools,
parents: noteParentStructure,
parents: noteParentsPublic,
});
});

Expand Down
21 changes: 8 additions & 13 deletions src/repository/storage/postgres/orm/sequelize/note.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import type { CreationOptional, InferAttributes, InferCreationAttributes, ModelStatic, NonAttribute, Sequelize } from 'sequelize';
import { DataTypes, Model } from 'sequelize';
import { DataTypes, Model, Op } from 'sequelize';
import type Orm from '@repository/storage/postgres/orm/sequelize/index.js';
import type { Note, NoteCreationAttributes, NoteInternalId, NotePublicId } from '@domain/entities/note.js';
import { UserModel } from '@repository/storage/postgres/orm/sequelize/user.js';
import type { NoteSettingsModel } from './noteSettings.js';
import type { NoteVisitsModel } from './noteVisits.js';
import type { NoteHistoryModel } from './noteHistory.js';
import { notEmpty } from '@infrastructure/utils/empty.js';

/* eslint-disable @typescript-eslint/naming-convention */

Expand Down Expand Up @@ -330,17 +329,13 @@ export default class NoteSequelizeStorage {
* @param noteIds - list of note ids
*/
public async getNotesByIds(noteIds: NoteInternalId[]): Promise<Note[]> {
const notes: Note[] = [];

for (const noteId of noteIds) {
const note: Note | null = await this.model.findOne({
where: { id: noteId },
});

if (notEmpty(note)) {
notes.push(note);
}
}
const notes: Note[] = await this.model.findAll({
where: {
id: {
[Op.in]: noteIds,
},
},
});

return notes;
}
Expand Down
22 changes: 8 additions & 14 deletions src/repository/storage/postgres/orm/sequelize/noteRelations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,6 @@ export default class NoteRelationsSequelizeStorage {
* @param noteId - the ID of the note.
*/
public async getNoteParentsIds(noteId: NoteInternalId): Promise<NoteInternalId[]> {
let parentNotes: NoteInternalId[] = [];

// get all note ids via a singe sql query instead of many
const query = `
WITH RECURSIVE note_parents AS (
Expand All @@ -233,20 +231,16 @@ export default class NoteRelationsSequelizeStorage {
SELECT np.note_id, np.parent_id
FROM note_parents np;`;

try {
const result = await this.database.query(query, {
replacements: { startNoteId: noteId },
type: QueryTypes.SELECT,
});
const result = await this.database.query(query, {
replacements: { startNoteId: noteId },
type: QueryTypes.SELECT,
});

// eslint-disable-next-line @typescript-eslint/naming-convention
parentNotes = (result as { note_id: number; parent_id: number }[])?.map(note => note.parent_id) ?? [];
// eslint-disable-next-line @typescript-eslint/naming-convention
let noteParents = (result as { note_id: number; parent_id: number }[])?.map(note => note.parent_id) ?? [];

parentNotes.reverse();
} catch {
console.log(`something wrong happened with sql query`);
}
noteParents.reverse();

return parentNotes;
return noteParents;
}
}

0 comments on commit 7bfe0eb

Please sign in to comment.