paricafe/src/models/repositories/user-group.ts

64 lines
1.6 KiB
TypeScript
Raw Normal View History

2019-05-18 06:36:33 -05:00
import { EntityRepository, Repository } from 'typeorm';
import { UserGroup } from '../entities/user-group.js';
import { UserGroupJoinings } from '../index.js';
import { SchemaType } from '@/misc/schema.js';
2019-05-18 06:36:33 -05:00
export type PackedUserGroup = SchemaType<typeof packedUserGroupSchema>;
@EntityRepository(UserGroup)
export class UserGroupRepository extends Repository<UserGroup> {
public async pack(
src: UserGroup['id'] | UserGroup,
): Promise<PackedUserGroup> {
2021-02-13 00:33:38 -06:00
const userGroup = typeof src === 'object' ? src : await this.findOneOrFail(src);
2019-05-18 06:36:33 -05:00
const users = await UserGroupJoinings.find({
userGroupId: userGroup.id
});
return {
id: userGroup.id,
createdAt: userGroup.createdAt.toISOString(),
name: userGroup.name,
2019-05-21 22:58:44 -05:00
ownerId: userGroup.userId,
2019-05-18 06:36:33 -05:00
userIds: users.map(x => x.userId)
};
}
}
export const packedUserGroupSchema = {
2019-06-27 04:04:09 -05:00
type: 'object' as const,
optional: false as const, nullable: false as const,
2019-05-18 06:36:33 -05:00
properties: {
id: {
2019-06-27 04:04:09 -05:00
type: 'string' as const,
optional: false as const, nullable: false as const,
2019-05-18 06:36:33 -05:00
format: 'id',
example: 'xxxxxxxxxx',
},
createdAt: {
2019-06-27 04:04:09 -05:00
type: 'string' as const,
optional: false as const, nullable: false as const,
2019-05-18 06:36:33 -05:00
format: 'date-time',
},
name: {
2019-06-27 04:04:09 -05:00
type: 'string' as const,
optional: false as const, nullable: false as const,
2019-05-18 06:36:33 -05:00
},
2019-05-21 22:58:44 -05:00
ownerId: {
2019-06-27 04:04:09 -05:00
type: 'string' as const,
nullable: false as const, optional: false as const,
2019-05-21 15:06:52 -05:00
format: 'id',
},
2019-05-18 06:36:33 -05:00
userIds: {
2019-06-27 04:04:09 -05:00
type: 'array' as const,
nullable: false as const, optional: true as const,
2019-05-18 06:36:33 -05:00
items: {
2019-06-27 04:04:09 -05:00
type: 'string' as const,
nullable: false as const, optional: false as const,
2019-05-18 06:36:33 -05:00
format: 'id',
}
},
},
};