|
@@ -6,6 +6,8 @@ import { Chat } from '../entity/chat.entity';
|
|
|
import { cloneDeep, get, head } from 'lodash';
|
|
|
import { ChatMqService } from './chatMq.service';
|
|
|
import { Types } from 'mongoose';
|
|
|
+import { JwtService } from '@midwayjs/jwt';
|
|
|
+const assert = require('assert');
|
|
|
const ObjectId = Types.ObjectId;
|
|
|
type modelType = ReturnModelType<typeof Chat>;
|
|
|
@Provide()
|
|
@@ -15,11 +17,18 @@ export class ChatService extends BaseService<modelType> {
|
|
|
@Inject()
|
|
|
chatMqService: ChatMqService;
|
|
|
|
|
|
+ @Inject()
|
|
|
+ jwtService: JwtService;
|
|
|
+
|
|
|
// 处理已读问题
|
|
|
async allRead(data) {
|
|
|
- // 群/病人/医生/发言者
|
|
|
- const { group, patient, doctor, speaker } = data;
|
|
|
- const result = await this.model.find({ group, patient, doctor, speaker: { $ne: speaker }, not_read: 1 }).lean();
|
|
|
+ const token = get(this.ctx, 'request.header.token');
|
|
|
+ assert(token, '缺少token信息');
|
|
|
+ const tokenInfo = await this.jwtService.decodeSync(token);
|
|
|
+ const info = { ...data, not_read: 1, speaker: { $ne: get(tokenInfo, '_id') } };
|
|
|
+ let result;
|
|
|
+ if (get(tokenInfo, 'role') === 'Patient') result = await this.model.find({ ...info }).lean();
|
|
|
+ else result = await this.model.find({ ...info, speaker_type: 'Patient' }).lean();
|
|
|
const ids = result.map(i => new ObjectId(i._id).toString());
|
|
|
await this.model.updateMany({ _id: ids }, { $set: { not_read: 0 } });
|
|
|
}
|