2019-05-18 20:36:33 +09:00
|
|
|
import { EntityRepository, Repository } from 'typeorm';
|
2021-08-19 18:33:41 +09:00
|
|
|
import { UserGroup } from '../entities/user-group.js';
|
|
|
|
import { UserGroupJoinings } from '../index.js';
|
|
|
|
import { SchemaType } from '@/misc/schema.js';
|
2019-05-18 20:36:33 +09:00
|
|
|
|
|
|
|
export type PackedUserGroup = SchemaType<typeof packedUserGroupSchema>;
|
|
|
|
|
|
|
|
@EntityRepository(UserGroup)
|
|
|
|
export class UserGroupRepository extends Repository<UserGroup> {
|
|
|
|
public async pack(
|
|
|
|
src: UserGroup['id'] | UserGroup,
|
|
|
|
): Promise<PackedUserGroup> {
|
2021-02-13 15:33:38 +09:00
|
|
|
const userGroup = typeof src === 'object' ? src : await this.findOneOrFail(src);
|
2019-05-18 20:36:33 +09:00
|
|
|
|
|
|
|
const users = await UserGroupJoinings.find({
|
|
|
|
userGroupId: userGroup.id
|
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
|
|
|
id: userGroup.id,
|
|
|
|
createdAt: userGroup.createdAt.toISOString(),
|
|
|
|
name: userGroup.name,
|
2019-05-22 12:58:44 +09:00
|
|
|
ownerId: userGroup.userId,
|
2019-05-18 20:36:33 +09:00
|
|
|
userIds: users.map(x => x.userId)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const packedUserGroupSchema = {
|
2019-06-27 18:04:09 +09:00
|
|
|
type: 'object' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-05-18 20:36:33 +09:00
|
|
|
properties: {
|
|
|
|
id: {
|
2019-06-27 18:04:09 +09:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-05-18 20:36:33 +09:00
|
|
|
format: 'id',
|
|
|
|
example: 'xxxxxxxxxx',
|
|
|
|
},
|
|
|
|
createdAt: {
|
2019-06-27 18:04:09 +09:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-05-18 20:36:33 +09:00
|
|
|
format: 'date-time',
|
|
|
|
},
|
|
|
|
name: {
|
2019-06-27 18:04:09 +09:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-05-18 20:36:33 +09:00
|
|
|
},
|
2019-05-22 12:58:44 +09:00
|
|
|
ownerId: {
|
2019-06-27 18:04:09 +09:00
|
|
|
type: 'string' as const,
|
|
|
|
nullable: false as const, optional: false as const,
|
2019-05-22 05:06:52 +09:00
|
|
|
format: 'id',
|
|
|
|
},
|
2019-05-18 20:36:33 +09:00
|
|
|
userIds: {
|
2019-06-27 18:04:09 +09:00
|
|
|
type: 'array' as const,
|
|
|
|
nullable: false as const, optional: true as const,
|
2019-05-18 20:36:33 +09:00
|
|
|
items: {
|
2019-06-27 18:04:09 +09:00
|
|
|
type: 'string' as const,
|
|
|
|
nullable: false as const, optional: false as const,
|
2019-05-18 20:36:33 +09:00
|
|
|
format: 'id',
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|