2019-04-07 21:50:36 +09:00
|
|
|
import { EntityRepository, Repository } from 'typeorm';
|
|
|
|
import { UserList } from '../entities/user-list';
|
2019-04-13 01:43:22 +09:00
|
|
|
import { ensure } from '../../prelude/ensure';
|
2019-04-13 17:26:38 +09:00
|
|
|
import { UserListJoinings } from '..';
|
2019-04-23 22:35:26 +09:00
|
|
|
import { bool, types, SchemaType } from '../../misc/schema';
|
|
|
|
|
|
|
|
export type PackedUserList = SchemaType<typeof packedUserListSchema>;
|
2019-04-07 21:50:36 +09:00
|
|
|
|
|
|
|
@EntityRepository(UserList)
|
|
|
|
export class UserListRepository extends Repository<UserList> {
|
|
|
|
public async pack(
|
2019-04-13 01:43:22 +09:00
|
|
|
src: UserList['id'] | UserList,
|
2019-04-23 22:35:26 +09:00
|
|
|
): Promise<PackedUserList> {
|
2019-04-13 01:43:22 +09:00
|
|
|
const userList = typeof src === 'object' ? src : await this.findOne(src).then(ensure);
|
2019-04-07 21:50:36 +09:00
|
|
|
|
2019-04-13 17:26:38 +09:00
|
|
|
const users = await UserListJoinings.find({
|
|
|
|
userListId: userList.id
|
|
|
|
});
|
|
|
|
|
2019-04-07 21:50:36 +09:00
|
|
|
return {
|
|
|
|
id: userList.id,
|
2019-04-23 22:35:26 +09:00
|
|
|
createdAt: userList.createdAt.toISOString(),
|
2019-04-13 17:26:38 +09:00
|
|
|
name: userList.name,
|
|
|
|
userIds: users.map(x => x.userId)
|
2019-04-07 21:50:36 +09:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2019-04-23 22:35:26 +09:00
|
|
|
|
|
|
|
export const packedUserListSchema = {
|
|
|
|
type: types.object,
|
|
|
|
optional: bool.false, nullable: bool.false,
|
|
|
|
properties: {
|
|
|
|
id: {
|
|
|
|
type: types.string,
|
|
|
|
optional: bool.false, nullable: bool.false,
|
|
|
|
format: 'id',
|
|
|
|
description: 'The unique identifier for this UserList.',
|
|
|
|
example: 'xxxxxxxxxx',
|
|
|
|
},
|
|
|
|
createdAt: {
|
|
|
|
type: types.string,
|
|
|
|
optional: bool.false, nullable: bool.false,
|
|
|
|
format: 'date-time',
|
|
|
|
description: 'The date that the UserList was created.'
|
|
|
|
},
|
|
|
|
name: {
|
|
|
|
type: types.string,
|
|
|
|
optional: bool.false, nullable: bool.false,
|
|
|
|
description: 'The name of the UserList.'
|
|
|
|
},
|
|
|
|
userIds: {
|
|
|
|
type: types.array,
|
|
|
|
nullable: bool.false, optional: bool.true,
|
|
|
|
items: {
|
|
|
|
type: types.string,
|
|
|
|
nullable: bool.false, optional: bool.false,
|
|
|
|
format: 'id',
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|