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