This repository has been archived by the owner on Apr 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 118
/
TeamMember.ts
114 lines (95 loc) · 2.46 KB
/
TeamMember.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
102
103
104
105
106
107
108
109
110
111
112
113
114
import {
Table,
Column,
AllowNull,
IsEmail,
BelongsTo,
BeforeSave,
ForeignKey,
IsLowercase,
BeforeValidate,
BeforeDestroy,
AfterCreate,
} from "sequelize-typescript";
import { LoggedModel } from "../classes/loggedModel";
import { Team } from "./Team";
import { TeamMemberOps } from "../modules/ops/teamMember";
import { LockableHelper } from "../modules/lockableHelper";
import { APIData } from "../modules/apiData";
import { CLS } from "../modules/cls";
@Table({ tableName: "teamMembers", paranoid: false })
export class TeamMember extends LoggedModel<TeamMember> {
idPrefix() {
return "tem";
}
@AllowNull(false)
@Column
@ForeignKey(() => Team)
teamId: string;
@AllowNull(true)
@Column
locked: string;
@AllowNull(false)
@Column
firstName: string;
@AllowNull(false)
@Column
lastName: string;
@AllowNull(false)
@IsEmail
@IsLowercase
@Column
email: string;
@Column
passwordHash: string;
@Column
lastLoginAt: Date;
@BelongsTo(() => Team)
team: Team;
async apiData() {
return {
id: this.id,
teamId: this.teamId,
firstName: this.firstName,
lastName: this.lastName,
email: this.email,
locked: this.locked,
lastLoginAt: this.lastLoginAt,
createdAt: APIData.formatDate(this.createdAt),
updatedAt: APIData.formatDate(this.updatedAt),
};
}
async updatePassword(password: string) {
return TeamMemberOps.updatePassword(this, password);
}
async checkPassword(password: string) {
return TeamMemberOps.checkPassword(this, password);
}
// --- Class Methods --- //
static async findById(id: string) {
const instance = await this.scope(null).findOne({ where: { id } });
if (!instance) throw new Error(`cannot find ${this.name} ${id}`);
return instance;
}
@BeforeValidate
static lowercaseEmail(instance: TeamMember) {
if (instance.email) instance.email = instance.email.toLocaleLowerCase();
}
@BeforeSave
static async ensureTeamExists(instance: TeamMember) {
const team = await instance.$get("team");
if (!team) throw new Error("team not found");
}
@BeforeSave
static async noUpdateIfLocked(instance) {
await LockableHelper.beforeSave(instance, ["lastLoginAt"]);
}
@AfterCreate
static async sendTelemetry() {
await CLS.enqueueTask("telemetry:adHoc", { trigger: "team" });
}
@BeforeDestroy
static async noDestroyIfLocked(instance) {
await LockableHelper.beforeDestroy(instance);
}
}