summaryrefslogtreecommitdiff
path: root/src/models/repositories/user-list.ts
blob: 54231c7f851e803044b675b24c3a29ee5d8cfd17 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import { EntityRepository, Repository } from 'typeorm';
import { UserList } from '../entities/user-list';
import { ensure } from '../../prelude/ensure';
import { UserListJoinings } from '..';
import { bool, types, SchemaType } from '../../misc/schema';

export type PackedUserList = SchemaType<typeof packedUserListSchema>;

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

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

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

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',
			}
		},
	},
};