Skip to content

Commit

Permalink
Merge pull request #272 from codex-team/delete-note-history-on-note-d…
Browse files Browse the repository at this point in the history
…eletion

chore(noteHistory): add user data to the noteHistoryMeta
  • Loading branch information
e11sy authored Jul 27, 2024
2 parents 1fa4a8d + 8d32f47 commit ed1c796
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 11 deletions.
20 changes: 19 additions & 1 deletion src/domain/entities/noteHistory.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -42,7 +54,13 @@ export type NoteHistoryCreationAttributes = Omit<NoteHistoryRecord, 'id' | 'crea
* Meta data of the note history record
* Used for presentation of the note history record in web
*/
export type NoteHistoryMeta = Omit<NoteHistoryRecord, 'content' | 'noteId' | 'tools'>;
export type NoteHistoryMeta = Omit<NoteHistoryRecord, 'content' | 'noteId' | 'tools'> & {
/**
* 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
Expand Down
13 changes: 10 additions & 3 deletions src/presentation/http/router/note.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
});
}
});
Expand Down
13 changes: 13 additions & 0 deletions src/presentation/http/schema/History.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
},
},
},
},
};
22 changes: 17 additions & 5 deletions src/repository/storage/postgres/orm/sequelize/noteHistory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ export class NoteHistoryModel extends Model<InferAttributes<NoteHistoryModel>, 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;

Expand Down Expand Up @@ -96,7 +96,7 @@ export default class NoteHistorySequelizeStorage {

this.model.belongsTo(this.userModel, {
foreignKey: 'userId',
as: this.userModel.tableName,
as: 'user',
});
}

Expand All @@ -109,7 +109,7 @@ export default class NoteHistorySequelizeStorage {

this.model.belongsTo(this.noteModel, {
foreignKey: 'noteId',
as: this.noteModel.tableName,
as: 'note',
});
}

Expand Down Expand Up @@ -139,10 +139,22 @@ export default class NoteHistorySequelizeStorage {
* @returns array of metadata of the history records
*/
public async getNoteHistoryByNoteId(noteId: NoteHistoryRecord['noteId']): Promise<NoteHistoryMeta[]> {
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[];
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/tests/utils/database-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit ed1c796

Please sign in to comment.