import { ObjectId } from 'mongodb' import { CMSUser, DocumentHistoryItem } from './../../../types/index.d' import { GET, QRequestController, SECURE, } from './../../modules/RequestController' export default class HistoryController extends QRequestController { @GET @SECURE() async fetch() { const { id } = this.query if (!id) { return this.answer(false, undefined, 'Нет id') } const history = (await this.db .collection('system_documents_history') .find({ documentId: id }) .sort({ date: -1 }) .toArray()) as DocumentHistoryItem[] const usersIds = [...new Set(history.map((h) => h.byId))].map( (id) => new ObjectId(id) ) const users = (await this.db .collection('users') .find({ _id: { $in: usersIds } }) .toArray()) as CMSUser[] for (const item of history) { item.user = users.find((u) => u._id.toString() === item.byId) } return this.answer(true, history) } }