Skip to content

Commit 5cfaacf

Browse files
committed
feat(user service): add school details when user with school role logs in
1 parent 9c72ef6 commit 5cfaacf

File tree

2 files changed

+103
-30
lines changed

2 files changed

+103
-30
lines changed

src/user/fusionauth/fusionauth.service.ts

Lines changed: 93 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import FusionAuthClient, {
33
LoginResponse,
44
RegistrationRequest,
55
RegistrationResponse,
6+
SearchRequest,
7+
SearchResponse,
8+
Sort,
69
UUID,
710
User,
811
UserRegistration,
@@ -129,36 +132,45 @@ export class FusionauthService {
129132
update(
130133
userID: UUID,
131134
authObj: any,
135+
isSimpleUpdate = false,
132136
): Promise<{ statusFA: FAStatus; userId: UUID; fusionAuthUser: User }> {
133-
const registrations: Array<UserRegistration> = [];
134-
const currentRegistration: UserRegistration = {
135-
username: authObj.username,
136-
applicationId: process.env.FUSIONAUTH_APPLICATION_ID,
137-
roles: authObj.role,
138-
};
139-
registrations.push(currentRegistration);
140-
const userRequest: UserRequest = {
141-
user: {
142-
active: true,
143-
data: {
144-
school: authObj.school,
145-
education: authObj.education,
146-
address: authObj.address,
147-
gender: authObj.gender,
148-
dateOfRetirement: authObj.dateOfRetirement,
149-
phoneVerified: false,
150-
udise: authObj.udise,
151-
},
152-
email: authObj.email,
153-
firstName: authObj.firstName,
154-
lastName: authObj.lastName,
155-
fullName: authObj.fullName,
137+
let userRequest: UserRequest;
138+
if (!isSimpleUpdate) {
139+
const registrations: Array<UserRegistration> = [];
140+
const currentRegistration: UserRegistration = {
156141
username: authObj.username,
157-
password: authObj.password,
158-
imageUrl: authObj.avatar,
159-
mobilePhone: authObj.phone,
160-
},
161-
};
142+
applicationId: process.env.FUSIONAUTH_APPLICATION_ID,
143+
roles: authObj.role,
144+
};
145+
registrations.push(currentRegistration);
146+
147+
userRequest = {
148+
user: {
149+
active: true,
150+
data: {
151+
school: authObj.school,
152+
education: authObj.education,
153+
address: authObj.address,
154+
gender: authObj.gender,
155+
dateOfRetirement: authObj.dateOfRetirement,
156+
phoneVerified: false,
157+
udise: authObj.udise,
158+
},
159+
email: authObj.email,
160+
firstName: authObj.firstName,
161+
lastName: authObj.lastName,
162+
fullName: authObj.fullName,
163+
username: authObj.username,
164+
password: authObj.password,
165+
imageUrl: authObj.avatar,
166+
mobilePhone: authObj.phone,
167+
},
168+
};
169+
} else {
170+
userRequest = {
171+
user: authObj,
172+
};
173+
}
162174

163175
return this.fusionauthClient
164176
.patchUser(userID, userRequest)
@@ -189,4 +201,57 @@ export class FusionauthService {
189201
verifyUsernamePhoneCombination(): Promise<boolean> {
190202
return Promise.resolve(true);
191203
}
204+
205+
//One time Task
206+
async updateAllEmptyRolesToSchool(): Promise<any> {
207+
let allDone = false;
208+
const searchRequest: SearchRequest = {
209+
search: {
210+
numberOfResults: 15,
211+
startRow: 0,
212+
sortFields: [
213+
{
214+
missing: '_first',
215+
name: 'id',
216+
order: Sort.asc,
217+
},
218+
],
219+
query:
220+
'{"bool":{"must":[{"nested":{"path":"registrations","query":{"bool":{"must":[{"match":{"registrations.applicationId":"f0ddb3f6-091b-45e4-8c0f-889f89d4f5da"}}],"must_not":[{"match":{"registrations.roles":"school"}}]}}}}]}}',
221+
},
222+
};
223+
let iteration = 0;
224+
let invalidUsersCount = 0;
225+
while (!allDone) {
226+
iteration += 1;
227+
searchRequest.search.startRow = invalidUsersCount;
228+
const resp: ClientResponse<SearchResponse> =
229+
await this.fusionauthClient.searchUsersByQuery(searchRequest);
230+
const total = resp.response.total;
231+
console.log(iteration, total);
232+
if (total === 0) allDone = true;
233+
else {
234+
const users: Array<User> = resp.response.users;
235+
for (const user of users) {
236+
if (user.registrations[0].roles === undefined) {
237+
user.registrations[0].roles = ['school'];
238+
console.log('Here', user);
239+
await this.fusionauthClient
240+
.updateRegistration(user.id, {
241+
registration: user.registrations[0],
242+
})
243+
.then((resp) => {
244+
console.log('response', JSON.stringify(resp));
245+
})
246+
.catch((e) => {
247+
console.log('error', JSON.stringify(e));
248+
});
249+
} else {
250+
console.log('Invalid User', user.id);
251+
invalidUsersCount += 1;
252+
}
253+
}
254+
}
255+
}
256+
}
192257
}

src/user/user.service.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,14 +248,22 @@ export class UserService {
248248
return this.fusionAuthService
249249
.login(user)
250250
.then(async (resp: ClientResponse<LoginResponse>) => {
251-
const fusionAuthUser: LoginResponse = resp.response;
252-
console.log(fusionAuthUser.user.registrations[0].roles);
251+
let fusionAuthUser: LoginResponse = resp.response;
253252
if (this.isOldSchoolUser(fusionAuthUser.user)) {
253+
//updateUserData with school and udise
254254
fusionAuthUser.user.data = {};
255255
const udise = fusionAuthUser.user.username;
256256
fusionAuthUser.user.data.udise = udise;
257257
const schoolId = await this.userDBService.getSchool(udise);
258258
fusionAuthUser.user.data.school = schoolId.id;
259+
await this.fusionAuthService.update(
260+
fusionAuthUser.user.id,
261+
fusionAuthUser.user,
262+
true,
263+
);
264+
265+
//login again to get new JWT
266+
fusionAuthUser = (await this.fusionAuthService.login(user)).response;
259267
const response: SignupResponse = new SignupResponse().init(uuidv4());
260268
response.responseCode = ResponseCode.OK;
261269
response.result = {

0 commit comments

Comments
 (0)