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

68 lines
1.8 KiB
TypeScript
Raw Normal View History

2019-05-18 20:36:33 +09:00
import { EntityRepository, Repository } from 'typeorm';
import { UserGroup } from '../entities/user-group';
import { ensure } from '../../prelude/ensure';
import { UserGroupJoinings } from '..';
2019-06-27 18:04:09 +09:00
import { SchemaType } from '../../misc/schema';
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> {
const userGroup = typeof src === 'object' ? src : await this.findOne(src).then(ensure);
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',
description: 'The unique identifier for this UserGroup.',
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',
description: 'The date that the UserGroup was created.'
},
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
description: 'The name of the UserGroup.'
},
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',
}
},
},
};