Skip to content

Commit a15f244

Browse files
authored
Merge pull request #273 from codex-team/delete-note-history-on-note-deletion
chore(note-history): Changed created_at field of the note_history table
2 parents ed1c796 + 4618333 commit a15f244

5 files changed

Lines changed: 52 additions & 9 deletions

File tree

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
DO $$
2+
BEGIN
3+
-- Check if field created_at exists in public.note_history
4+
IF EXISTS (
5+
SELECT 1
6+
FROM information_schema.columns
7+
WHERE table_schema = 'public'
8+
AND table_name = 'note_history'
9+
AND column_name = 'created_at'
10+
) THEN
11+
-- Changing field type to timestamp with time zone
12+
ALTER TABLE public.note_history
13+
ALTER COLUMN created_at TYPE timestamp with time zone
14+
USING created_at AT TIME ZONE 'UTC';
15+
16+
-- Update current values of created_at field
17+
UPDATE public.note_history
18+
SET created_at = created_at AT TIME ZONE 'UTC' AT TIME ZONE 'Europe/Moscow'; -- Замените 'Europe/Moscow' на нужный вам часовой пояс
19+
END IF;
20+
END $$;

src/domain/entities/noteHistory.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,12 @@ export type NoteHistoryMeta = Omit<NoteHistoryRecord, 'content' | 'noteId' | 'to
6262
user: UserMeta;
6363
};
6464

65+
/**
66+
* Entity that represents NoteHistoryRecord with user info
67+
*/
68+
export type NoteHistoryView = NoteHistoryRecord & { user: UserMeta };
69+
6570
/**
6671
* Public note history record with note public id instead of note internal id
6772
*/
68-
export type NoteHistoryPublic = Omit<NoteHistoryRecord, 'noteId'> & { noteId: NotePublicId };
73+
export type NoteHistoryPublic = Omit<NoteHistoryView, 'noteId'> & { noteId: NotePublicId };

src/domain/service/note.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ export default class NoteService {
416416
* Get concrete history record of the note
417417
* Used for showing some of the note content versions
418418
* @param id - id of the note history record
419-
* @returns full public note history record or raises domain error if record not found
419+
* @returns full public note history record with user information or raises domain error if record not found
420420
*/
421421
public async getHistoryRecordById(id: NoteHistoryRecord['id']): Promise<NoteHistoryPublic> {
422422
const noteHistoryRecord = await this.noteHistoryRepository.getHistoryRecordById(id);
@@ -436,6 +436,7 @@ export default class NoteService {
436436
content: noteHistoryRecord.content,
437437
tools: noteHistoryRecord.tools,
438438
createdAt: noteHistoryRecord.createdAt,
439+
user: noteHistoryRecord.user,
439440
};
440441

441442
return noteHistoryPublic;

src/repository/noteHistory.repository.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { NoteHistoryCreationAttributes, NoteHistoryMeta, NoteHistoryRecord } from '@domain/entities/noteHistory.js';
1+
import type { NoteHistoryCreationAttributes, NoteHistoryMeta, NoteHistoryRecord, NoteHistoryView } from '@domain/entities/noteHistory.js';
22
import type NoteHistoryStorage from '@repository/storage/noteHistory.storage.js';
33

44
/**
@@ -36,9 +36,9 @@ export default class NoteHistoryRepository {
3636
* Get concrete history record by it's id
3737
* Used for presentation of certain version of note content saved in history
3838
* @param id - id of the history record
39-
* @returns full history record or null if there is no record with such an id
39+
* @returns full history record with user information or null if there is no record with such an id
4040
*/
41-
public async getHistoryRecordById(id: NoteHistoryRecord['id']): Promise<NoteHistoryRecord | null> {
41+
public async getHistoryRecordById(id: NoteHistoryRecord['id']): Promise<NoteHistoryView | null> {
4242
return await this.storage.getHistoryRecordById(id);
4343
}
4444

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

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { DataTypes, literal, Model } from 'sequelize';
33
import type Orm from '@repository/storage/postgres/orm/sequelize/index.js';
44
import { NoteModel } from './note.js';
55
import { UserModel } from './user.js';
6-
import type { NoteHistoryCreationAttributes, NoteHistoryRecord, NoteHistoryMeta } from '@domain/entities/noteHistory.js';
6+
import type { NoteHistoryCreationAttributes, NoteHistoryRecord, NoteHistoryMeta, NoteHistoryView } from '@domain/entities/noteHistory.js';
77

88
/**
99
* Note history model instance
@@ -147,6 +147,7 @@ export default class NoteHistorySequelizeStorage {
147147
as: 'user',
148148
attributes: ['name', 'photo'],
149149
},
150+
order: [['createdAt', 'DESC']],
150151
});
151152

152153
/**
@@ -160,10 +161,26 @@ export default class NoteHistorySequelizeStorage {
160161
/**
161162
* Get concrete history record by it's id
162163
* @param id - id of the history record
163-
* @returns full history record or null if there is no record with such an id
164+
* @returns full history record with user information or null if there is no record with such an id
164165
*/
165-
public async getHistoryRecordById(id: NoteHistoryRecord['id']): Promise<NoteHistoryRecord | null> {
166-
return await this.model.findByPk(id);
166+
public async getHistoryRecordById(id: NoteHistoryRecord['id']): Promise<NoteHistoryView | null> {
167+
const historyView = await this.model.findOne({
168+
where: {
169+
id,
170+
},
171+
include: {
172+
model: this.userModel,
173+
as: 'user',
174+
attributes: ['name', 'photo'],
175+
},
176+
});
177+
178+
/**
179+
* We need this cast because of using sequelize model.include
180+
* Since it returns NoteHistoryModel[], however we included userModel,
181+
* without this cast NoteHistoryModel[] and NoteHistoryView are incompatible
182+
*/
183+
return historyView as unknown as NoteHistoryView;
167184
}
168185

169186
/**

0 commit comments

Comments
 (0)