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

62 lines
1.5 KiB
TypeScript
Raw Normal View History

2019-05-18 20:36:33 +09:00
import { EntityRepository, Repository } from 'typeorm';
import { UserGroup } from '@/models/entities/user-group';
import { UserGroupJoinings } from '../index';
import { Packed } from '@/misc/schema';
2019-05-18 20:36:33 +09:00
@EntityRepository(UserGroup)
export class UserGroupRepository extends Repository<UserGroup> {
public async pack(
src: UserGroup['id'] | UserGroup,
): Promise<Packed<'UserGroup'>> {
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',
}
},
},
};