update
This commit is contained in:
@@ -5,6 +5,7 @@ import { PrismaService } from '../infra/prisma.service';
|
||||
import { SettingsService } from './settings.service';
|
||||
|
||||
import { NotificationsService } from './notifications.service';
|
||||
import { MinioService } from '../infra/minio.service';
|
||||
|
||||
|
||||
|
||||
@@ -22,7 +23,9 @@ export class FamilyService {
|
||||
|
||||
private readonly settings: SettingsService,
|
||||
|
||||
private readonly notifications: NotificationsService
|
||||
private readonly notifications: NotificationsService,
|
||||
|
||||
private readonly minio: MinioService
|
||||
|
||||
) {}
|
||||
|
||||
@@ -158,12 +161,140 @@ export class FamilyService {
|
||||
|
||||
this.assertOwner(group, requesterId);
|
||||
|
||||
await this.prisma.familyGroup.delete({ where: { id: groupId } });
|
||||
await this.destroyGroupWithCleanup(group);
|
||||
|
||||
return { count: 1 };
|
||||
|
||||
}
|
||||
|
||||
async destroyOwnedGroupsForUser(userId: string) {
|
||||
|
||||
const groups = await this.prisma.familyGroup.findMany({
|
||||
|
||||
where: { ownerId: userId },
|
||||
|
||||
include: { members: { include: { user: true } } }
|
||||
|
||||
});
|
||||
|
||||
for (const group of groups) {
|
||||
|
||||
await this.destroyGroupWithCleanup(group);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
async leaveAllMembershipsForUser(userId: string) {
|
||||
|
||||
const memberships = await this.prisma.familyMember.findMany({
|
||||
|
||||
where: { userId, role: { not: 'owner' } }
|
||||
|
||||
});
|
||||
|
||||
for (const member of memberships) {
|
||||
|
||||
await this.removeMember(userId, member.id);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private async destroyGroupWithCleanup(group: {
|
||||
|
||||
id: string;
|
||||
|
||||
ownerId: string;
|
||||
|
||||
name: string;
|
||||
|
||||
avatarStorageKey: string | null;
|
||||
|
||||
members: Array<{ userId: string }>;
|
||||
|
||||
}) {
|
||||
|
||||
const rooms = await this.prisma.chatRoom.findMany({
|
||||
|
||||
where: { groupId: group.id },
|
||||
|
||||
include: { messages: { select: { storageKey: true } } }
|
||||
|
||||
});
|
||||
|
||||
const storageKeys: string[] = [];
|
||||
|
||||
if (group.avatarStorageKey) {
|
||||
|
||||
storageKeys.push(group.avatarStorageKey);
|
||||
|
||||
}
|
||||
|
||||
for (const room of rooms) {
|
||||
|
||||
if (room.avatarStorageKey) {
|
||||
|
||||
storageKeys.push(room.avatarStorageKey);
|
||||
|
||||
}
|
||||
|
||||
for (const message of room.messages) {
|
||||
|
||||
if (message.storageKey) {
|
||||
|
||||
storageKeys.push(message.storageKey);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
await this.minio.deleteObjects(storageKeys);
|
||||
|
||||
await this.prisma.familyGroup.delete({ where: { id: group.id } });
|
||||
|
||||
for (const member of group.members) {
|
||||
|
||||
await this.notifications.publishRealtime(
|
||||
|
||||
member.userId,
|
||||
|
||||
'family_group_deleted',
|
||||
|
||||
'Семья удалена',
|
||||
|
||||
`Семья «${group.name}» была удалена`,
|
||||
|
||||
{ groupId: group.id, groupName: group.name }
|
||||
|
||||
);
|
||||
|
||||
if (member.userId === group.ownerId) {
|
||||
|
||||
continue;
|
||||
|
||||
}
|
||||
|
||||
await this.notifications.create(
|
||||
|
||||
member.userId,
|
||||
|
||||
'family_group_deleted',
|
||||
|
||||
'Семья удалена',
|
||||
|
||||
`Семья «${group.name}» была удалена создателем`,
|
||||
|
||||
{ groupId: group.id, groupName: group.name }
|
||||
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
async addMember(groupId: string, userId: string, role: string) {
|
||||
|
||||
Reference in New Issue
Block a user