Skip to content

Commit e90678e

Browse files
authored
Merge pull request #26 from Vidyaranya-Gavai-TTPL/revokeInvitation
Revoke Invitation
2 parents 3deaec5 + d3a4203 commit e90678e

File tree

4 files changed

+71
-52
lines changed

4 files changed

+71
-52
lines changed

src/common/utils/response.messages.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,6 @@ export const API_RESPONSES = {
6969
INVITEE_ONLY: "Invitation status can be updated by invitee only",
7070
INVITEE_ALREADY_MAPPED:
7171
"This user is already mapped as cohort_admin to respective cohort",
72+
UNAUTHORIZED_TO_REVOKE: "This user is unauthorized to revoke this invitation",
73+
REVOKE_ONLY_PENDING: "Only pending invitations can be revoked"
7274
};

src/invitation/dto/update-invitation.dto.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { IsEnum, IsOptional } from "class-validator";
55
export class UpdateInvitationDto {
66
@ApiProperty({ type: String, description: "Status of invitation" })
77
@IsOptional()
8-
@IsEnum(["Pending", "Accepted", "Rejected"])
8+
@IsEnum(["Pending", "Accepted", "Rejected", "Revoked"])
99
@Expose()
10-
invitationStatus: "Pending" | "Accepted" | "Rejected";
10+
invitationStatus: "Pending" | "Accepted" | "Rejected" | "Revoked";
1111
}

src/invitation/entities/invitation.entity.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ export class Invitations {
2626

2727
@Column({
2828
type: "enum",
29-
enum: ["Pending", "Accepted", "Rejected"],
29+
enum: ["Pending", "Accepted", "Rejected", "Revoked"],
3030
default: "Pending",
3131
})
32-
invitationStatus: "Pending" | "Accepted" | "Rejected";
32+
invitationStatus: "Pending" | "Accepted" | "Rejected" | "Revoked";
3333

3434
@CreateDateColumn({
3535
type: "timestamp with time zone",

src/invitation/invitation.service.ts

Lines changed: 65 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { UserRoleMapping } from 'src/rbac/assign-role/entities/assign-role.entit
1010
import { User } from 'src/user/entities/user-entity';
1111
import { Tenants } from 'src/userTenantMapping/entities/tenant.entity';
1212
import { UserTenantMapping } from 'src/userTenantMapping/entities/user-tenant-mapping.entity';
13-
import { Repository } from 'typeorm';
13+
import { Not, Repository } from 'typeorm';
1414
import { API_RESPONSES } from '@utils/response.messages';
1515
import { Invitations } from './entities/invitation.entity';
1616
import { CohortMembers } from 'src/cohortMembers/entities/cohort-member.entity';
@@ -181,7 +181,7 @@ export class InvitationService {
181181

182182
let sentInvitations = [];
183183
sentInvitations = await this.invitationsRepository.find({
184-
where: { invitedBy: email },
184+
where: { invitedBy: email, invitationStatus: Not("Revoked") },
185185
});
186186

187187
// Add cohort name response
@@ -272,62 +272,79 @@ export class InvitationService {
272272
);
273273
}
274274

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;
304279
return APIResponse.error(
305280
response,
306281
apiId,
307-
API_RESPONSES.INVITEE_ALREADY_MAPPED,
282+
API_RESPONSES.INVITEE_ONLY,
308283
error,
309-
HttpStatus.CONFLICT
284+
HttpStatus.UNAUTHORIZED
310285
);
311286
}
312287

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+
};
321319

322-
await this.postgresUserService.assignUserToTenant(tenantsData, null);
320+
await this.postgresUserService.assignUserToTenant(tenantsData, null);
323321

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+
};
329327

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+
)
331348
}
332349

333350
// update invitation status

0 commit comments

Comments
 (0)