doctor-mail/apps/server/src/models/post/post.service.ts

181 lines
4.9 KiB
TypeScript
Raw Normal View History

2024-12-30 08:26:40 +08:00
import { Injectable } from '@nestjs/common';
import {
db,
Prisma,
UserProfile,
Post,
RolePerms,
ResPerm,
ObjectType,
2025-01-24 15:06:57 +08:00
PostType,
2025-01-06 08:45:23 +08:00
} from '@nice/common';
2024-12-30 08:26:40 +08:00
import { MessageService } from '../message/message.service';
import { BaseService } from '../base/base.service';
import { DepartmentService } from '../department/department.service';
2025-01-24 15:06:57 +08:00
import { setPostRelation, updatePostState } from './utils';
2024-12-30 08:26:40 +08:00
import EventBus, { CrudOperation } from '@server/utils/event-bus';
@Injectable()
export class PostService extends BaseService<Prisma.PostDelegate> {
constructor(
private readonly messageService: MessageService,
private readonly departmentService: DepartmentService,
) {
super(db, ObjectType.POST);
}
2025-01-24 15:06:57 +08:00
onModuleInit() {
EventBus.on('updatePostState', ({ id }) => {
console.log('updatePostState');
updatePostState(id);
});
}
2024-12-30 08:26:40 +08:00
async create(
args: Prisma.PostCreateArgs,
params: { staff?: UserProfile; tx?: Prisma.PostDelegate },
) {
2025-01-25 19:51:08 +08:00
console.log('params?.staff?.id', params?.staff?.id);
2024-12-31 15:57:32 +08:00
args.data.authorId = params?.staff?.id;
2025-01-24 00:19:02 +08:00
args.data.updatedAt = new Date();
2025-01-22 19:25:55 +08:00
// args.data.resources
2024-12-30 08:26:40 +08:00
const result = await super.create(args);
2025-01-24 00:19:02 +08:00
await this.updateParentTimestamp(result?.parentId);
2024-12-30 08:26:40 +08:00
EventBus.emit('dataChanged', {
type: ObjectType.POST,
operation: CrudOperation.CREATED,
data: result,
});
2025-01-24 15:06:57 +08:00
if (args.data.authorId && args?.data?.type === PostType.POST_COMMENT) {
EventBus.emit('updatePostState', {
id: result?.id,
});
}
2024-12-30 08:26:40 +08:00
return result;
}
async update(args: Prisma.PostUpdateArgs, staff?: UserProfile) {
2024-12-31 15:57:32 +08:00
args.data.authorId = staff?.id;
2025-01-24 00:19:02 +08:00
args.data.updatedAt = new Date();
2025-01-03 09:24:46 +08:00
const result = await super.update(args);
EventBus.emit('dataChanged', {
type: ObjectType.POST,
operation: CrudOperation.UPDATED,
data: result,
});
2025-01-22 18:56:27 +08:00
return result;
2024-12-30 08:26:40 +08:00
}
2025-01-24 17:39:41 +08:00
async findFirst(
args?: Prisma.PostFindFirstArgs,
staff?: UserProfile,
clientIp?: string,
) {
2025-01-24 00:19:02 +08:00
const transDto = await this.wrapResult(
super.findFirst(args),
async (result) => {
2025-01-24 17:39:41 +08:00
await setPostRelation({ data: result, staff, clientIp });
2025-01-24 00:19:02 +08:00
await this.setPerms(result, staff);
return result;
},
);
return transDto;
}
2025-01-24 17:39:41 +08:00
async findManyWithCursor(
args: Prisma.PostFindManyArgs,
staff?: UserProfile,
clientIp?: string,
) {
2025-01-22 18:56:27 +08:00
if (!args.where) args.where = {};
2024-12-30 08:26:40 +08:00
args.where.OR = await this.preFilter(args.where.OR, staff);
return this.wrapResult(super.findManyWithCursor(args), async (result) => {
2025-01-22 18:56:27 +08:00
const { items } = result;
2024-12-30 08:26:40 +08:00
await Promise.all(
items.map(async (item) => {
2025-01-24 17:39:41 +08:00
await setPostRelation({ data: item, staff, clientIp });
2024-12-30 08:26:40 +08:00
await this.setPerms(item, staff);
}),
);
2025-01-24 17:39:41 +08:00
2024-12-30 08:26:40 +08:00
return { ...result, items };
});
}
2024-12-31 15:57:32 +08:00
protected async setPerms(data: Post, staff?: UserProfile) {
2024-12-30 08:26:40 +08:00
if (!staff) return;
const perms: ResPerm = {
delete: false,
};
const isMySelf = data?.authorId === staff?.id;
2025-01-22 18:56:27 +08:00
// const isDomain = staff.domainId === data.domainId;
2024-12-30 08:26:40 +08:00
const setManagePermissions = (perms: ResPerm) => {
Object.assign(perms, {
delete: true,
// edit: true,
});
};
if (isMySelf) {
perms.delete = true;
// perms.edit = true;
}
staff.permissions.forEach((permission) => {
switch (permission) {
case RolePerms.MANAGE_ANY_POST:
setManagePermissions(perms);
break;
2025-01-22 18:56:27 +08:00
// case RolePerms.MANAGE_DOM_POST:
// if (isDomain) {
// setManagePermissions(perms);
// }
// break;
2024-12-30 08:26:40 +08:00
}
});
Object.assign(data, { perms });
}
async preFilter(OR?: Prisma.PostWhereInput[], staff?: UserProfile) {
const preFilter = (await this.getPostPreFilter(staff)) || [];
const outOR = OR ? [...OR, ...preFilter].filter(Boolean) : preFilter;
return outOR?.length > 0 ? outOR : undefined;
}
2024-12-31 15:57:32 +08:00
async getPostPreFilter(staff?: UserProfile) {
2025-01-22 18:56:27 +08:00
if (!staff) return;
// const { deptId, domainId } = staff;
2024-12-30 08:26:40 +08:00
if (
staff.permissions.includes(RolePerms.READ_ANY_POST) ||
staff.permissions.includes(RolePerms.MANAGE_ANY_POST)
) {
return undefined;
}
const orCondition: Prisma.PostWhereInput[] = [
staff?.id && {
authorId: staff.id,
},
2025-01-22 18:56:27 +08:00
{
isPublic: true,
},
2024-12-30 08:26:40 +08:00
staff?.id && {
2025-01-22 18:56:27 +08:00
receivers: {
2024-12-30 08:26:40 +08:00
some: {
id: staff.id,
},
},
},
].filter(Boolean);
if (orCondition?.length > 0) return orCondition;
return undefined;
}
2025-01-24 00:19:02 +08:00
/**
*
*
* @param parentId ID
*/
async updateParentTimestamp(parentId: string | undefined) {
if (!parentId) {
return;
}
await this.update({
where: {
id: parentId,
},
data: {}, // 空对象会自动更新 updatedAt 时间戳
});
}
2024-12-30 08:26:40 +08:00
}