2025-05-14 21:45:16 +02:00

116 lines
3.4 KiB
TypeScript

import { ObjectType, Field, ID, ArgsType, InputType, Int } from 'type-graphql';
import { BaseGraphModel } from '@seed/graphql/BaseGraphModel';
import { Permission } from '@seed/interfaces/permission';
import AccountModel from '@src/accounts/account.model';
import { GetArgs } from '@seed/graphql/Request';
import { ApolloContext } from '@seed/interfaces/context';
import { ApolloError } from 'apollo-server-lambda';
import { Stats } from 'fs';
import { RoomsStats } from '@services/api-messaging/components';
import MessageModel from '../messages/message.model';
import { AccountTypeEnum } from '@src/accounts/account.components';
import _ from 'lodash';
const permissions: Permission = {
c: [AccountTypeEnum.admin],
r: [AccountTypeEnum.admin, AccountTypeEnum.public],
w: [AccountTypeEnum.admin],
d: [AccountTypeEnum.admin],
};
@ObjectType()
export default class RoomModel extends BaseGraphModel {
public constructor() {
super({
// ...init,
collectionName: 'messaging.rooms',
permissions: permissions,
});
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
@Field(() => ID)
readonly _id: string;
@Field(() => String, { nullable: true })
title?: string;
@Field(() => RoomsStats)
stats: RoomsStats;
@Field(() => [AccountModel], { nullable: true })
getAccounts?: AccountModel[];
@Field(() => [MessageModel], { nullable: true })
getMessages?: MessageModel[];
@Field({ nullable: true })
lastMessage: Date;
searchOptions(): string[] {
return ['title'];
}
filterOptions(): string[] {
return ['title'];
}
async updateUnreadCounts(newMessage: MessageModel, count: 1 | -1 | 0): Promise<void> {
const { sentBy } = newMessage;
for (let index = 0; index < this.stats.unreadCounts.length; index++) {
// Add a count to all the other one
if (this.stats.unreadCounts[index].accountId != sentBy) {
if (count == 0) this.stats.unreadCounts[index].count = 0;
else this.stats.unreadCounts[index].count = this.stats.unreadCounts[index].count + count;
}
if (this.stats.unreadCounts[index].count < 0) this.stats.unreadCounts[index].count = 0;
}
await (await this.db()).findOneAndUpdate(
{ _id: this.get()._id },
{ $set: { stats: this.stats } as any },
{ upsert: false, returnOriginal: false },
);
}
}
@ArgsType()
export class RoomArgs {
@Field(() => GetArgs, { nullable: true })
pagination?: GetArgs;
}
@InputType()
export class NewRoomInput {
@Field(() => [String])
accountIds: string[];
}
@InputType()
export class AddPeopleToRoomInput {
@Field()
roomId: string;
@Field(() => [String])
accountIds: string[];
}
@InputType()
export class RemovePeopleFromRoomInput {
@Field()
roomId: string;
@Field(() => [String])
accountIds: string[];
}
export const onCreate = async (input: NewRoomInput, ctx: ApolloContext): Promise<void> => {
try {
// To refactor
const users = await ctx.ctx.loaders.accountLoader.loadMany(input.accountIds);
input.accountIds.forEach((element) => {
if (_.findIndex(users, { _id: element }) == -1) throw new ApolloError('users.notFound', '400', { _id: element });
});
} catch (error) {
throw error;
}
};