@@ -3,7 +3,7 @@ import { DataTypes, literal, Model } from 'sequelize';
33import type Orm from '@repository/storage/postgres/orm/sequelize/index.js' ;
44import { NoteModel } from './note.js' ;
55import { 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