2022-03-26 06:34:00 +00:00
|
|
|
import { db } from '@/db/postgre.js';
|
2022-02-27 02:07:39 +00:00
|
|
|
import { UserGroup } from '@/models/entities/user-group.js';
|
|
|
|
import { UserGroupJoinings } from '../index.js';
|
|
|
|
import { Packed } from '@/misc/schema.js';
|
2019-05-18 11:36:33 +00:00
|
|
|
|
2022-03-26 06:34:00 +00:00
|
|
|
export const UserGroupRepository = db.getRepository(UserGroup).extend({
|
|
|
|
async pack(
|
2019-05-18 11:36:33 +00:00
|
|
|
src: UserGroup['id'] | UserGroup,
|
2021-09-22 13:35:55 +00:00
|
|
|
): Promise<Packed<'UserGroup'>> {
|
2022-03-26 06:34:00 +00:00
|
|
|
const userGroup = typeof src === 'object' ? src : await this.findOneByOrFail({ id: src });
|
2019-05-18 11:36:33 +00:00
|
|
|
|
2022-03-26 06:34:00 +00:00
|
|
|
const users = await UserGroupJoinings.findBy({
|
2021-12-09 14:58:30 +00:00
|
|
|
userGroupId: userGroup.id,
|
2019-05-18 11:36:33 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
|
|
|
id: userGroup.id,
|
|
|
|
createdAt: userGroup.createdAt.toISOString(),
|
|
|
|
name: userGroup.name,
|
2019-05-22 03:58:44 +00:00
|
|
|
ownerId: userGroup.userId,
|
2021-12-09 14:58:30 +00:00
|
|
|
userIds: users.map(x => x.userId),
|
2019-05-18 11:36:33 +00:00
|
|
|
};
|
2022-03-26 06:34:00 +00:00
|
|
|
},
|
|
|
|
});
|