@@ -10,7 +10,7 @@ import { UserRoleMapping } from 'src/rbac/assign-role/entities/assign-role.entit
10
10
import { User } from 'src/user/entities/user-entity' ;
11
11
import { Tenants } from 'src/userTenantMapping/entities/tenant.entity' ;
12
12
import { UserTenantMapping } from 'src/userTenantMapping/entities/user-tenant-mapping.entity' ;
13
- import { Repository } from 'typeorm' ;
13
+ import { Not , Repository } from 'typeorm' ;
14
14
import { API_RESPONSES } from '@utils/response.messages' ;
15
15
import { Invitations } from './entities/invitation.entity' ;
16
16
import { CohortMembers } from 'src/cohortMembers/entities/cohort-member.entity' ;
@@ -181,7 +181,7 @@ export class InvitationService {
181
181
182
182
let sentInvitations = [ ] ;
183
183
sentInvitations = await this . invitationsRepository . find ( {
184
- where : { invitedBy : email } ,
184
+ where : { invitedBy : email , invitationStatus : Not ( "Revoked" ) } ,
185
185
} ) ;
186
186
187
187
// Add cohort name response
@@ -272,62 +272,79 @@ export class InvitationService {
272
272
) ;
273
273
}
274
274
275
- // Only invitee can accept or reject (update status)
276
- if ( invitation . invitedTo !== email ) {
277
- const error = API_RESPONSES . INVITEE_ONLY ;
278
- return APIResponse . error (
279
- response ,
280
- apiId ,
281
- API_RESPONSES . INVITEE_ONLY ,
282
- error ,
283
- HttpStatus . UNAUTHORIZED
284
- ) ;
285
- }
286
-
287
- // If accepted, then map user as cohort admin
288
- if ( updateInvitationDto . invitationStatus === "Accepted" ) {
289
- // Get role for roleId
290
- const role = await this . roleRepository . findOne ( {
291
- where : { tenantId : invitation . tenantId , code : "cohort_admin" } ,
292
- } ) ;
293
-
294
- // Check if user is already mapped as cohort admin for given cohort or not
295
- const userRoleMap = await this . userRoleMappingRepository . findOne ( {
296
- where : { userId, roleId : role . roleId } ,
297
- } ) ;
298
- const cohortMember = await this . cohortMembersRepository . findOne ( {
299
- where : { cohortId : invitation . cohortId , userId } ,
300
- } ) ;
301
-
302
- if ( userRoleMap && cohortMember ) {
303
- const error = API_RESPONSES . INVITEE_ALREADY_MAPPED ;
275
+ if ( updateInvitationDto . invitationStatus !== 'Revoked' ) {
276
+ // Only invitee can accept or reject (update status)
277
+ if ( invitation . invitedTo !== email ) {
278
+ const error = API_RESPONSES . INVITEE_ONLY ;
304
279
return APIResponse . error (
305
280
response ,
306
281
apiId ,
307
- API_RESPONSES . INVITEE_ALREADY_MAPPED ,
282
+ API_RESPONSES . INVITEE_ONLY ,
308
283
error ,
309
- HttpStatus . CONFLICT
284
+ HttpStatus . UNAUTHORIZED
310
285
) ;
311
286
}
312
287
313
- // Assign user to tenant with appropriate role
314
- const tenantsData = {
315
- tenantRoleMapping : {
316
- tenantId : invitation . tenantId ,
317
- roleId : role . roleId ,
318
- } ,
319
- userId : userId ,
320
- } ;
288
+ // If accepted, then map user as cohort admin
289
+ if ( invitation . invitationStatus === "Accepted" ) {
290
+ // Get role for roleId
291
+ const role = await this . roleRepository . findOne ( {
292
+ where : { tenantId : invitation . tenantId , code : "cohort_admin" } ,
293
+ } ) ;
294
+
295
+ // Check if user is already mapped as cohort admin or not
296
+ const userRoleMap = await this . userRoleMappingRepository . findOne ( {
297
+ where : { userId, roleId : role . roleId } ,
298
+ } ) ;
299
+
300
+ if ( userRoleMap ) {
301
+ const error = API_RESPONSES . INVITEE_ALREADY_MAPPED ;
302
+ return APIResponse . error (
303
+ response ,
304
+ apiId ,
305
+ API_RESPONSES . INVITEE_ALREADY_MAPPED ,
306
+ error ,
307
+ HttpStatus . CONFLICT
308
+ ) ;
309
+ }
310
+
311
+ // Assign user to tenant with appropriate role
312
+ const tenantsData = {
313
+ tenantRoleMapping : {
314
+ tenantId : invitation . tenantId ,
315
+ roleId : role . roleId ,
316
+ } ,
317
+ userId : userId ,
318
+ } ;
321
319
322
- await this . postgresUserService . assignUserToTenant ( tenantsData , null ) ;
320
+ await this . postgresUserService . assignUserToTenant ( tenantsData , null ) ;
323
321
324
- // Add user as cohort member
325
- const cohortData = {
326
- userId : userId ,
327
- cohortId : invitation . cohortId ,
328
- } ;
322
+ // Add user as cohort member
323
+ const cohortData = {
324
+ userId : userId ,
325
+ cohortId : invitation . cohortId ,
326
+ } ;
329
327
330
- await this . postgresUserService . addCohortMember ( cohortData ) ;
328
+ await this . postgresUserService . addCohortMember ( cohortData ) ;
329
+ }
330
+ } else if ( invitation . invitedBy !== email ) {
331
+ const errorMessage = API_RESPONSES . UNAUTHORIZED_TO_REVOKE
332
+ return APIResponse . error (
333
+ response ,
334
+ apiId ,
335
+ API_RESPONSES . UNAUTHORIZED_TO_REVOKE ,
336
+ errorMessage ,
337
+ HttpStatus . UNAUTHORIZED
338
+ )
339
+ } else if ( invitation . invitationStatus !== "Pending" ) {
340
+ const errorMessage = API_RESPONSES . REVOKE_ONLY_PENDING
341
+ return APIResponse . error (
342
+ response ,
343
+ apiId ,
344
+ API_RESPONSES . REVOKE_ONLY_PENDING ,
345
+ errorMessage ,
346
+ HttpStatus . UNAUTHORIZED
347
+ )
331
348
}
332
349
333
350
// update invitation status
0 commit comments