-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathteams.ts
249 lines (225 loc) · 6.29 KB
/
teams.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
import type { Sequelize, InferAttributes, InferCreationAttributes, CreationOptional, ModelStatic } from 'sequelize';
import { Model, DataTypes } from 'sequelize';
import type Orm from '@repository/storage/postgres/orm/sequelize/index.js';
import { NoteModel } from '@repository/storage/postgres/orm/sequelize/note.js';
import type { Team, TeamMemberCreationAttributes, TeamMember } from '@domain/entities/team.js';
import { UserModel } from './user.js';
import { MemberRole } from '@domain/entities/team.js';
import type User from '@domain/entities/user.js';
import type { NoteInternalId } from '@domain/entities/note.js';
/**
* Class representing a teams model in database
*/
export class TeamsModel extends Model<InferAttributes<TeamsModel>, InferCreationAttributes<TeamsModel>> {
/**
* team member id
*/
public declare id: CreationOptional<TeamMember['id']>;
/**
* Note ID
*/
public declare noteId: TeamMember['noteId'];
/**
* Team member user id
*/
public declare userId: TeamMember['userId'];
/**
* Team member role, show what user can do with note
*/
public declare role: MemberRole;
}
/**
* Class representing a table storing note teams
*/
export default class TeamsSequelizeStorage {
/**
* Team model in database
*/
public model: typeof TeamsModel;
/**
* Database instance
*/
private readonly database: Sequelize;
/**
* Note model instance
*/
private noteModel: typeof NoteModel | null = null;
/**
* User model instance
*/
private userModel: typeof UserModel | null = null;
/**
* Teams table name
*/
private readonly tableName = 'note_teams';
/**
* Constructor for note storage
* @param ormInstance - ORM instance
*/
constructor({ connection }: Orm) {
this.database = connection;
/**
* Initiate note note teams model
*/
this.model = TeamsModel.init({
id: {
type: DataTypes.INTEGER,
autoIncrement: true,
primaryKey: true,
},
noteId: {
type: DataTypes.INTEGER,
allowNull: false,
references: {
model: NoteModel,
key: 'id',
},
},
userId: {
type: DataTypes.INTEGER,
allowNull: false,
references: {
model: UserModel,
key: 'id',
},
},
role: {
type: DataTypes.INTEGER,
allowNull: false,
defaultValue: MemberRole.Read,
},
}, {
tableName: this.tableName,
sequelize: this.database,
timestamps: false,
indexes: [
// Create a unique index on noteId and userId
{
unique: true,
fields: ['note_id', 'user_id'],
},
],
});
}
/**
* Creates association with note model to make joins
* @param model - initialized note model
*/
public createAssociationWithNoteModel(model: ModelStatic<NoteModel>): void {
this.noteModel = model;
/**
* Make one-to-one association with note model
* We can not create team relation without note
*/
this.model.belongsTo(model, {
foreignKey: 'noteId',
as: this.noteModel.tableName,
});
}
/**
* Creates association with user model to make joins
* @param model - initialized user model
*/
public createAssociationWithUserModel(model: ModelStatic<UserModel>): void {
this.userModel = model;
/**
* Make one-to-one association with user model
* We can not create team relation without user
*/
this.model.belongsTo(model, {
foreignKey: 'userId',
as: 'user',
});
}
/**
* Create new team member membership
* @param data - team membership data
*/
public async createTeamMembership(data: TeamMemberCreationAttributes): Promise<TeamMember> {
return await this.model.create({
noteId: data.noteId,
userId: data.userId,
role: data.role,
});
}
/**
* Get team member by user id and note id
* @param userId - user id to check
* @param noteId - note id to identify team
* @returns return null if user is not in team, teamMember otherwhise
*/
public async getTeamMemberByNoteAndUserId(userId: User['id'], noteId: NoteInternalId): Promise<TeamMember | null> {
return await this.model.findOne({
where: {
noteId,
userId,
},
});
}
/**
* Get all team members by note id
* @param noteId - note id to get all team members
* @returns team relations
*/
public async getMembersByNoteId(noteId: NoteInternalId): Promise<Team> {
return await this.model.findAll({
where: {
noteId,
},
});
}
/**
* Get all team members by note id with info about users
* @param noteId - note id to get all team members
* @returns team with additional info
*/
public async getTeamMembersWithUserInfoByNoteId(noteId: NoteInternalId): Promise<Team> {
if (!this.userModel) {
throw new Error('TeamStorage: User model not defined');
}
return await this.model.findAll({
where: { noteId },
attributes: ['id', 'role'],
include: {
model: this.userModel,
as: 'user',
required: true,
attributes: ['id', 'name', 'email', 'photo'],
},
});
}
/**
* Remove team member by id
* @param userId - id of team member
* @param noteId - note internal id
* @returns returns userId if team member was deleted and undefined overwise
*/
public async removeTeamMemberByUserIdAndNoteId(userId: TeamMember['id'], noteId: NoteInternalId): Promise<User['id'] | undefined> {
const affectedRows = await this.model.destroy({
where: {
userId,
noteId,
},
});
return affectedRows > 0 ? userId : undefined;
}
/**
* Patch team member role by user and note id
* @param userId - id of team member
* @param noteId - note internal id
* @param role - new team member role
* @returns returns 1 if the role has been changed and 0 otherwise
*/
public async patchMemberRoleById(userId: TeamMember['id'], noteId: NoteInternalId, role: MemberRole): Promise<MemberRole | undefined> {
const affectedRows = await this.model.update({
role: role,
}, {
where: {
userId,
noteId,
},
});
// if counter of affected rows is more than 0, then we return new role
return affectedRows[0] ? role : undefined;
}
}