Skip to content

Commit ed1c796

Browse files
authored
Merge pull request #272 from codex-team/delete-note-history-on-note-deletion
chore(noteHistory): add user data to the noteHistoryMeta
2 parents 1fa4a8d + 8d32f47 commit ed1c796

5 files changed

Lines changed: 61 additions & 11 deletions

File tree

src/domain/entities/noteHistory.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
11
import type { NoteInternalId, Note, NotePublicId } from './note.js';
22
import type User from './user.js';
33

4+
interface UserMeta {
5+
/**
6+
* Name of the user
7+
*/
8+
name: User['name'];
9+
10+
/**
11+
* Photo of the user
12+
*/
13+
photo: User['photo'];
14+
};
15+
416
export interface NoteHistoryRecord {
517
/**
618
* Unique identified of note history record
@@ -42,7 +54,13 @@ export type NoteHistoryCreationAttributes = Omit<NoteHistoryRecord, 'id' | 'crea
4254
* Meta data of the note history record
4355
* Used for presentation of the note history record in web
4456
*/
45-
export type NoteHistoryMeta = Omit<NoteHistoryRecord, 'content' | 'noteId' | 'tools'>;
57+
export type NoteHistoryMeta = Omit<NoteHistoryRecord, 'content' | 'noteId' | 'tools'> & {
58+
/**
59+
* Meta data of the user who did changes
60+
* Used for note history metadata presentation
61+
*/
62+
user: UserMeta;
63+
};
4664

4765
/**
4866
* Public note history record with note public id instead of note internal id

src/presentation/http/router/note.test.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1977,15 +1977,22 @@ describe('Note API', () => {
19771977
expect(response?.json()).toStrictEqual({ message: expectedMessage });
19781978
} else {
19791979
expect(response?.json().noteHistoryMeta).toHaveLength(2);
1980-
expect(response?.json().noteHistoryMeta[0]).toMatchObject({
1981-
id: 1,
1980+
expect(response?.json().noteHistoryMeta[1]).toMatchObject({
19821981
userId: creator.id,
1982+
user: {
1983+
name: creator.name,
1984+
photo: creator.photo,
1985+
},
19831986
});
19841987

1985-
expect(response?.json().noteHistoryMeta[1]).toMatchObject({
1988+
expect(response?.json().noteHistoryMeta[0]).toMatchObject({
19861989
id: history.id,
19871990
userId: history.userId,
19881991
createdAt: history.createdAt,
1992+
user: {
1993+
name: creator.name,
1994+
photo: creator.photo,
1995+
},
19891996
});
19901997
}
19911998
});

src/presentation/http/schema/History.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,5 +84,18 @@ export const HistoryMetaSchema = {
8484
type: 'string',
8585
format: 'date-time',
8686
},
87+
user: {
88+
type: 'object',
89+
properties: {
90+
name: {
91+
description: 'name of the user',
92+
type: 'string',
93+
},
94+
photo: {
95+
description: 'photo of the user',
96+
type: 'string',
97+
},
98+
},
99+
},
87100
},
88101
};

src/repository/storage/postgres/orm/sequelize/noteHistory.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ export class NoteHistoryModel extends Model<InferAttributes<NoteHistoryModel>, I
4444
export default class NoteHistorySequelizeStorage {
4545
public model: typeof NoteHistoryModel;
4646

47-
public userModel: typeof UserModel | null = null;
47+
public userModel: typeof UserModel | undefined = undefined;
4848

49-
public noteModel: typeof NoteModel | null = null;
49+
public noteModel: typeof NoteModel | undefined = undefined;
5050

5151
private readonly database: Sequelize;
5252

@@ -96,7 +96,7 @@ export default class NoteHistorySequelizeStorage {
9696

9797
this.model.belongsTo(this.userModel, {
9898
foreignKey: 'userId',
99-
as: this.userModel.tableName,
99+
as: 'user',
100100
});
101101
}
102102

@@ -109,7 +109,7 @@ export default class NoteHistorySequelizeStorage {
109109

110110
this.model.belongsTo(this.noteModel, {
111111
foreignKey: 'noteId',
112-
as: this.noteModel.tableName,
112+
as: 'note',
113113
});
114114
}
115115

@@ -139,10 +139,22 @@ export default class NoteHistorySequelizeStorage {
139139
* @returns array of metadata of the history records
140140
*/
141141
public async getNoteHistoryByNoteId(noteId: NoteHistoryRecord['noteId']): Promise<NoteHistoryMeta[]> {
142-
return await this.model.findAll({
142+
const historyMeta = await this.model.findAll({
143143
where: { noteId },
144144
attributes: ['id', 'userId', 'createdAt'],
145+
include: {
146+
model: this.userModel,
147+
as: 'user',
148+
attributes: ['name', 'photo'],
149+
},
145150
});
151+
152+
/**
153+
* We need this cast because of using sequelize model.include
154+
* Since it returns NoteHistoryModel[], however we included userModel,
155+
* without this cast NoteHistoryModel[] and NoteHistoryMeta[] are incompatible
156+
*/
157+
return historyMeta as unknown as NoteHistoryMeta[];
146158
}
147159

148160
/**

src/tests/utils/database-helpers.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,8 @@ export default class DatabaseHelpers {
193193
const name = user?.name ?? `CodeX-${randomPart}`;
194194
const email = user?.email ?? `${randomPart}@codexmail.com`;
195195

196-
const [results, _] = await this.orm.connection.query(`INSERT INTO public.users ("email", "name", "created_at", "editor_tools")
197-
VALUES ('${email}', '${name}', CLOCK_TIMESTAMP(), '${editorTools}'::jsonb)
196+
const [results, _] = await this.orm.connection.query(`INSERT INTO public.users ("email", "name", "created_at", "editor_tools", "photo")
197+
VALUES ('${email}', '${name}', CLOCK_TIMESTAMP(), '${editorTools}'::jsonb, '')
198198
RETURNING "id", "email", "name", "editor_tools" AS "editorTools", "created_at" AS "createdAt", "photo"`,
199199
{
200200
type: QueryTypes.INSERT,

0 commit comments

Comments
 (0)