summaryrefslogtreecommitdiff
path: root/src/models/repositories/user-list.ts
blob: e591794b8b96756d7485aca030df324790b9cf9b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import { EntityRepository, Repository } from 'typeorm';
import { UserList } from '../entities/user-list';
import { ensure } from '../../prelude/ensure';
import { UserListJoinings } from '..';

@EntityRepository(UserList)
export class UserListRepository extends Repository<UserList> {
	public async pack(
		src: UserList['id'] | UserList,
	) {
		const userList = typeof src === 'object' ? src : await this.findOne(src).then(ensure);

		const users = await UserListJoinings.find({
			userListId: userList.id
		});

		return {
			id: userList.id,
			name: userList.name,
			userIds: users.map(x => x.userId)
		};
	}
}