-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUser.ts
101 lines (89 loc) · 2.6 KB
/
User.ts
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import Objection, { Model } from 'objection';
import { z } from 'zod';
import { BaseModel } from './BaseModel';
import { ModelObjectOpt } from './ModelObjectOpt';
import type { Participant } from './Participant';
import { UserToParticipantRole } from './UserToParticipantRole';
export interface IUser {}
export enum UserJobFunction {
BusinessDevelopment = 'Business Development',
DA = 'Data / Analytics',
Marketing = 'Marketing',
MediaBuyer = 'Media Buyer',
PM = 'Product Management',
Engineering = 'Engineering',
SCRelationships = 'Sales / Client Relationships',
TechnicalOps = 'Technical Ops',
Other = 'Other',
}
export class User extends BaseModel {
static get tableName() {
return 'users';
}
static get virtualAttributes() {
return ['fullName'];
}
fullName() {
return `${this.firstName} ${this.lastName}`;
}
static readonly relationMappings = {
participants: {
relation: Model.ManyToManyRelation,
modelClass: 'Participant',
join: {
from: 'users.id',
through: {
from: 'usersToParticipantRoles.userId',
to: 'usersToParticipantRoles.participantId',
},
to: 'participants.id',
},
},
userToParticipantRoles: {
relation: Model.HasManyRelation,
modelClass: 'UserToParticipantRole',
join: {
from: 'users.id',
to: 'usersToParticipantRoles.userId',
},
},
};
declare id: number;
declare email: string;
declare firstName: string;
declare lastName: string;
declare phone?: string;
declare jobFunction: UserJobFunction;
declare participants?: Participant[];
declare acceptedTerms: boolean;
declare locked?: boolean;
declare userToParticipantRoles?: UserToParticipantRole[];
static readonly modifiers = {
withParticipants<TResult>(query: Objection.QueryBuilder<User, TResult>) {
const myQuery = query.withGraphFetched(
'[participants.participantToUserRoles]'
) as Objection.QueryBuilder<User, TResult & { participants: Participant[] }>;
return myQuery;
},
};
}
export type UserDTO = ModelObjectOpt<User>;
export const UserSchema = z.object({
id: z.number(),
email: z.string(),
firstName: z.string(),
lastName: z.string(),
phone: z.string().optional(),
jobFunction: z.nativeEnum(UserJobFunction).optional(),
acceptedTerms: z.boolean(),
locked: z.boolean().optional(),
});
export const UserCreationPartial = UserSchema.pick({
email: true,
firstName: true,
lastName: true,
phone: true,
jobFunction: true,
acceptedTerms: true,
});
export type UserCreationDTO = Omit<ModelObjectOpt<UserDTO>, 'id'>;