2019-05-18 13:36:33 +02:00
|
|
|
import { EntityRepository, Repository } from 'typeorm';
|
|
|
|
import { UserGroup } from '../entities/user-group';
|
|
|
|
import { ensure } from '../../prelude/ensure';
|
|
|
|
import { UserGroupJoinings } from '..';
|
2019-06-27 11:04:09 +02:00
|
|
|
import { SchemaType } from '../../misc/schema';
|
2019-05-18 13:36:33 +02: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 05:58:44 +02:00
|
|
|
ownerId: userGroup.userId,
|
2019-05-18 13:36:33 +02:00
|
|
|
userIds: users.map(x => x.userId)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const packedUserGroupSchema = {
|
2019-06-27 11:04:09 +02:00
|
|
|
type: 'object' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-05-18 13:36:33 +02:00
|
|
|
properties: {
|
|
|
|
id: {
|
2019-06-27 11:04:09 +02:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-05-18 13:36:33 +02:00
|
|
|
format: 'id',
|
|
|
|
description: 'The unique identifier for this UserGroup.',
|
|
|
|
example: 'xxxxxxxxxx',
|
|
|
|
},
|
|
|
|
createdAt: {
|
2019-06-27 11:04:09 +02:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-05-18 13:36:33 +02:00
|
|
|
format: 'date-time',
|
|
|
|
description: 'The date that the UserGroup was created.'
|
|
|
|
},
|
|
|
|
name: {
|
2019-06-27 11:04:09 +02:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-05-18 13:36:33 +02:00
|
|
|
description: 'The name of the UserGroup.'
|
|
|
|
},
|
2019-05-22 05:58:44 +02:00
|
|
|
ownerId: {
|
2019-06-27 11:04:09 +02:00
|
|
|
type: 'string' as const,
|
|
|
|
nullable: false as const, optional: false as const,
|
2019-05-21 22:06:52 +02:00
|
|
|
format: 'id',
|
|
|
|
},
|
2019-05-18 13:36:33 +02:00
|
|
|
userIds: {
|
2019-06-27 11:04:09 +02:00
|
|
|
type: 'array' as const,
|
|
|
|
nullable: false as const, optional: true as const,
|
2019-05-18 13:36:33 +02:00
|
|
|
items: {
|
2019-06-27 11:04:09 +02:00
|
|
|
type: 'string' as const,
|
|
|
|
nullable: false as const, optional: false as const,
|
2019-05-18 13:36:33 +02:00
|
|
|
format: 'id',
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|