Skip to content

Commit ba8a276

Browse files
committed
added migration for created_at field change
1 parent 8d32f47 commit ba8a276

5 files changed

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

65+
export type NoteHistoryView = NoteHistoryRecord & { user: UserMeta };
66+
6567
/**
6668
* Public note history record with note public id instead of note internal id
6769
*/
68-
export type NoteHistoryPublic = Omit<NoteHistoryRecord, 'noteId'> & { noteId: NotePublicId };
70+
export type NoteHistoryPublic = Omit<NoteHistoryView, 'noteId'> & { noteId: NotePublicId };

src/domain/service/note.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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: 2 additions & 2 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
/**
@@ -38,7 +38,7 @@ export default class NoteHistoryRepository {
3838
* @param id - id of the history record
3939
* @returns full history record 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: 19 additions & 3 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
@@ -162,8 +162,24 @@ export default class NoteHistorySequelizeStorage {
162162
* @param id - id of the history record
163163
* @returns full history record or null if there is no record with such an id
164164
*/
165-
public async getHistoryRecordById(id: NoteHistoryRecord['id']): Promise<NoteHistoryRecord | null> {
166-
return await this.model.findByPk(id);
165+
public async getHistoryRecordById(id: NoteHistoryRecord['id']): Promise<NoteHistoryView | null> {
166+
const historyView = await this.model.findOne({
167+
where: {
168+
id,
169+
},
170+
include: {
171+
model: this.userModel,
172+
as: 'user',
173+
attributes: ['name', 'photo'],
174+
},
175+
});
176+
177+
/**
178+
* We need this cast because of using sequelize model.include
179+
* Since it returns NoteHistoryModel[], however we included userModel,
180+
* without this cast NoteHistoryModel[] and NoteHistoryView are incompatible
181+
*/
182+
return historyView as unknown as NoteHistoryView;
167183
}
168184

169185
/**

0 commit comments

Comments
 (0)