paricafe/packages/backend/src/models/repositories/user-group.ts

26 lines
747 B
TypeScript
Raw Normal View History

2019-05-18 06:36:33 -05:00
import { EntityRepository, Repository } from 'typeorm';
import { UserGroup } from '@/models/entities/user-group.js';
import { UserGroupJoinings } from '../index.js';
import { Packed } from '@/misc/schema.js';
2019-05-18 06:36:33 -05:00
@EntityRepository(UserGroup)
export class UserGroupRepository extends Repository<UserGroup> {
public async pack(
src: UserGroup['id'] | UserGroup,
): Promise<Packed<'UserGroup'>> {
2021-02-13 00:33:38 -06:00
const userGroup = typeof src === 'object' ? src : await this.findOneOrFail(src);
2019-05-18 06:36:33 -05:00
const users = await UserGroupJoinings.find({
2021-12-09 08:58:30 -06:00
userGroupId: userGroup.id,
2019-05-18 06:36:33 -05:00
});
return {
id: userGroup.id,
createdAt: userGroup.createdAt.toISOString(),
name: userGroup.name,
2019-05-21 22:58:44 -05:00
ownerId: userGroup.userId,
2021-12-09 08:58:30 -06:00
userIds: users.map(x => x.userId),
2019-05-18 06:36:33 -05:00
};
}
}