diff --git a/src/domain/entities/noteHistory.ts b/src/domain/entities/noteHistory.ts index 24088c6f..25d3323d 100644 --- a/src/domain/entities/noteHistory.ts +++ b/src/domain/entities/noteHistory.ts @@ -1,6 +1,18 @@ import type { NoteInternalId, Note, NotePublicId } from './note.js'; import type User from './user.js'; +interface UserMeta { + /** + * Name of the user + */ + name: User['name']; + + /** + * Photo of the user + */ + photo: User['photo']; +}; + export interface NoteHistoryRecord { /** * Unique identified of note history record @@ -42,7 +54,13 @@ export type NoteHistoryCreationAttributes = Omit; +export type NoteHistoryMeta = Omit & { + /** + * Meta data of the user who did changes + * Used for note history metadata presentation + */ + user: UserMeta; +}; /** * Public note history record with note public id instead of note internal id diff --git a/src/presentation/http/router/note.test.ts b/src/presentation/http/router/note.test.ts index e8727463..606d38d7 100644 --- a/src/presentation/http/router/note.test.ts +++ b/src/presentation/http/router/note.test.ts @@ -1977,15 +1977,22 @@ describe('Note API', () => { expect(response?.json()).toStrictEqual({ message: expectedMessage }); } else { expect(response?.json().noteHistoryMeta).toHaveLength(2); - expect(response?.json().noteHistoryMeta[0]).toMatchObject({ - id: 1, + expect(response?.json().noteHistoryMeta[1]).toMatchObject({ userId: creator.id, + user: { + name: creator.name, + photo: creator.photo, + }, }); - expect(response?.json().noteHistoryMeta[1]).toMatchObject({ + expect(response?.json().noteHistoryMeta[0]).toMatchObject({ id: history.id, userId: history.userId, createdAt: history.createdAt, + user: { + name: creator.name, + photo: creator.photo, + }, }); } }); diff --git a/src/presentation/http/schema/History.ts b/src/presentation/http/schema/History.ts index 63cd5d87..f9e18cb2 100644 --- a/src/presentation/http/schema/History.ts +++ b/src/presentation/http/schema/History.ts @@ -84,5 +84,18 @@ export const HistoryMetaSchema = { type: 'string', format: 'date-time', }, + user: { + type: 'object', + properties: { + name: { + description: 'name of the user', + type: 'string', + }, + photo: { + description: 'photo of the user', + type: 'string', + }, + }, + }, }, }; diff --git a/src/repository/storage/postgres/orm/sequelize/noteHistory.ts b/src/repository/storage/postgres/orm/sequelize/noteHistory.ts index 85cb30a7..a09f9f82 100644 --- a/src/repository/storage/postgres/orm/sequelize/noteHistory.ts +++ b/src/repository/storage/postgres/orm/sequelize/noteHistory.ts @@ -44,9 +44,9 @@ export class NoteHistoryModel extends Model, I export default class NoteHistorySequelizeStorage { public model: typeof NoteHistoryModel; - public userModel: typeof UserModel | null = null; + public userModel: typeof UserModel | undefined = undefined; - public noteModel: typeof NoteModel | null = null; + public noteModel: typeof NoteModel | undefined = undefined; private readonly database: Sequelize; @@ -96,7 +96,7 @@ export default class NoteHistorySequelizeStorage { this.model.belongsTo(this.userModel, { foreignKey: 'userId', - as: this.userModel.tableName, + as: 'user', }); } @@ -109,7 +109,7 @@ export default class NoteHistorySequelizeStorage { this.model.belongsTo(this.noteModel, { foreignKey: 'noteId', - as: this.noteModel.tableName, + as: 'note', }); } @@ -139,10 +139,22 @@ export default class NoteHistorySequelizeStorage { * @returns array of metadata of the history records */ public async getNoteHistoryByNoteId(noteId: NoteHistoryRecord['noteId']): Promise { - return await this.model.findAll({ + const historyMeta = await this.model.findAll({ where: { noteId }, attributes: ['id', 'userId', 'createdAt'], + include: { + model: this.userModel, + as: 'user', + attributes: ['name', 'photo'], + }, }); + + /** + * We need this cast because of using sequelize model.include + * Since it returns NoteHistoryModel[], however we included userModel, + * without this cast NoteHistoryModel[] and NoteHistoryMeta[] are incompatible + */ + return historyMeta as unknown as NoteHistoryMeta[]; } /** diff --git a/src/tests/utils/database-helpers.ts b/src/tests/utils/database-helpers.ts index 4d089789..0af4d1a9 100644 --- a/src/tests/utils/database-helpers.ts +++ b/src/tests/utils/database-helpers.ts @@ -193,8 +193,8 @@ export default class DatabaseHelpers { const name = user?.name ?? `CodeX-${randomPart}`; const email = user?.email ?? `${randomPart}@codexmail.com`; - const [results, _] = await this.orm.connection.query(`INSERT INTO public.users ("email", "name", "created_at", "editor_tools") - VALUES ('${email}', '${name}', CLOCK_TIMESTAMP(), '${editorTools}'::jsonb) + const [results, _] = await this.orm.connection.query(`INSERT INTO public.users ("email", "name", "created_at", "editor_tools", "photo") + VALUES ('${email}', '${name}', CLOCK_TIMESTAMP(), '${editorTools}'::jsonb, '') RETURNING "id", "email", "name", "editor_tools" AS "editorTools", "created_at" AS "createdAt", "photo"`, { type: QueryTypes.INSERT,