Skip to content

Commit 99daedc

Browse files
refactor: removed userId parameter from send invitation and get invitation api
Signed-off-by: pranalidhanavade <pranali.dhanavade@ayanworks.com>
1 parent 0bfd617 commit 99daedc

8 files changed

Lines changed: 72 additions & 45 deletions

File tree

apps/api-gateway/src/ecosystem/ecosystem.controller.ts

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,20 @@ import {
77
ApiUnauthorizedResponse
88
} from '@nestjs/swagger';
99
import { Body, Controller, Get, HttpStatus, Param, Post, Res, UseFilters, UseGuards } from '@nestjs/common';
10-
import { ApiResponseDto } from '../dtos/apiResponse.dto';
1110
import { Response } from 'express';
1211
import { AuthGuard } from '@nestjs/passport';
1312
import { CustomExceptionFilter } from '@credebl/common/exception-handler';
1413
import { EcosystemService } from './ecosystem.service';
1514
import { ForbiddenErrorDto } from '../dtos/forbidden-error.dto';
16-
import { IResponse } from '@credebl/common/interfaces/response.interface';
1715
import { OrgRoles } from 'libs/org-roles/enums';
1816
import { ResponseMessages } from '@credebl/common/response-messages';
1917
import { Roles } from '../authz/decorators/roles.decorator';
2018
import { UnauthorizedErrorDto } from '../dtos/unauthorized-error.dto';
2119
import { SendEcosystemCreateDto } from './dtos/send-ecosystem-invitation';
2220
import { OrgRolesGuard } from '../authz/guards/org-roles.guard';
21+
import { user } from '@prisma/client';
22+
import { User } from '../authz/decorators/user.decorator';
23+
import { ApiResponseDto } from '../dtos/apiResponse.dto';
2324

2425
@UseFilters(CustomExceptionFilter)
2526
@Controller('ecosystem')
@@ -41,28 +42,30 @@ export class EcosystemController {
4142
* @param userId The ID of the organization
4243
* @returns Success message
4344
*/
44-
@Post('/:userId/invitations')
45-
@Roles(OrgRoles.PLATFORM_ADMIN)
45+
@Post('/invitations')
46+
@Roles(OrgRoles.OWNER, OrgRoles.ADMIN)
4647
@ApiOperation({
4748
summary: 'Create ecosystem invitation',
48-
description: 'Create an invitation to user to create a new ecosystem'
49+
description: 'Invite a user to create an ecosystem'
4950
})
50-
@ApiResponse({ status: HttpStatus.CREATED, description: 'Success', type: ApiResponseDto })
51-
@UseGuards(AuthGuard('jwt'), OrgRolesGuard)
51+
@ApiResponse({
52+
status: HttpStatus.CREATED,
53+
description: 'Success',
54+
type: ApiResponseDto
55+
})
56+
@UseGuards(AuthGuard('jwt'))
5257
@ApiBearerAuth()
5358
async createInvitation(
5459
@Body() sendEcosystemCreateDto: SendEcosystemCreateDto,
55-
@Param('userId') userId: string,
60+
@User() reqUser: user,
5661
@Res() res: Response
5762
): Promise<Response> {
58-
await this.ecosystemService.inviteUserToCreateEcosystem(sendEcosystemCreateDto, userId);
63+
await this.ecosystemService.inviteUserToCreateEcosystem(sendEcosystemCreateDto, reqUser.id);
5964

60-
const finalResponse: IResponse = {
65+
return res.status(HttpStatus.CREATED).json({
6166
statusCode: HttpStatus.CREATED,
6267
message: ResponseMessages.ecosystem.success.createInvitation
63-
};
64-
65-
return res.status(HttpStatus.CREATED).json(finalResponse);
68+
});
6669
}
6770

6871
@Get('/:userId/invitations')

apps/api-gateway/src/ecosystem/ecosystem.service.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,13 @@ export class EcosystemService {
1616
* @param SendEcosystemCreateDto
1717
* @returns Ecosystem creation success
1818
*/
19-
async inviteUserToCreateEcosystem(dto: SendEcosystemCreateDto, userId: string): Promise<IEcosystemInvitations> {
19+
async inviteUserToCreateEcosystem(
20+
dto: SendEcosystemCreateDto,
21+
platformAdminId: string
22+
): Promise<IEcosystemInvitations> {
2023
return this.natsClient.sendNatsMessage(this.serviceProxy, 'invite-user-for-ecosystem-creation', {
2124
email: dto.email,
22-
userId
25+
platformAdminId
2326
});
2427
}
2528

apps/ecosystem/repositories/ecosystem.repository.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,25 @@ export class EcosystemRepository {
1515
* @returns orgInvitaionDetails
1616
*/
1717

18-
async createEcosystemInvitation(email: string, userId: string): Promise<ecosystem_invitations> {
18+
async createEcosystemInvitation(payload: {
19+
email: string;
20+
invitedUserId: string | null;
21+
platformAdminId: string;
22+
}): Promise<ecosystem_invitations> {
23+
const { email, invitedUserId, platformAdminId } = payload;
24+
1925
return this.prisma.ecosystem_invitations.create({
2026
data: {
2127
email,
22-
// FIXME: Change status to PENDING once invitation accept/reject implemented
23-
status: Invitation.ACCEPTED,
24-
createdBy: userId,
25-
lastChangedBy: userId,
26-
user: {
27-
connect: { id: userId }
28-
}
28+
// Change to PENDING when accept/reject flow is ready
29+
status: Invitation.PENDING,
30+
31+
// invited user (nullable)
32+
userId: invitedUserId,
33+
34+
// platform admin
35+
createdBy: platformAdminId,
36+
lastChangedBy: platformAdminId
2937
}
3038
});
3139
}

apps/ecosystem/src/ecosystem.controller.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ export class EcosystemController {
1616
*/
1717

1818
@MessagePattern({ cmd: 'invite-user-for-ecosystem-creation' })
19-
async inviteUserToCreateEcosystem(payload: { email: string; userId: string }): Promise<IEcosystemInvitations> {
19+
async inviteUserToCreateEcosystem(payload: {
20+
email: string;
21+
platformAdminId: string;
22+
}): Promise<IEcosystemInvitations> {
2023
return this.ecosystemService.inviteUserToCreateEcosystem(payload);
2124
}
2225

apps/ecosystem/src/ecosystem.service.ts

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,31 @@ export class EcosystemService {
3434
* @param userId
3535
* @returns
3636
*/
37-
async inviteUserToCreateEcosystem(payload: { email: string; userId: string }): Promise<IEcosystemInvitations> {
38-
const { email, userId } = payload;
39-
40-
if (!email || !userId) {
41-
throw new BadRequestException('Email or userId missing');
37+
async inviteUserToCreateEcosystem(payload: {
38+
email: string;
39+
platformAdminId: string;
40+
}): Promise<IEcosystemInvitations> {
41+
const { email, platformAdminId } = payload;
42+
43+
if (!email || !platformAdminId) {
44+
throw new BadRequestException('Email or platformAdminId missing');
4245
}
4346

44-
const invitation = await this.ecosystemRepository.createEcosystemInvitation(email, userId);
47+
// Find invited user
48+
const invitedUser = await this.prisma.user.findUnique({
49+
where: { email }
50+
});
51+
52+
const invitation = await this.ecosystemRepository.createEcosystemInvitation({
53+
email,
54+
invitedUserId: invitedUser?.id ?? null,
55+
platformAdminId
56+
});
4557

46-
const isUserExist = await this.checkUserExistInPlatform(email);
47-
const userData = await this.getUserUserId(userId);
58+
const isUserExist = Boolean(invitedUser);
59+
const adminData = await this.getUserUserId(platformAdminId);
4860

49-
await this.sendInviteEmailTemplate(email, userData.firstName, isUserExist);
61+
await this.sendInviteEmailTemplate(email, adminData.firstName, isUserExist);
5062

5163
return invitation;
5264
}

apps/ecosystem/templates/create-ecosystem.templates.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ export class CreateEcosystemInviteTemplate {
1919
? ``
2020
: `To get started, kindly register on ${process.env.PLATFORM_NAME} platform using this link:`;
2121

22-
const buttonText = isUserExist
23-
? `Sign in to ${process.env.PLATFORM_NAME}`
24-
: `Register on ${process.env.PLATFORM_NAME}`;
22+
const secondaryMessage = isUserExist ? `Please log in to the platform to start creating your ecosystem.` : ``;
23+
24+
const buttonText = isUserExist ? `Create Ecosystem` : `Register on ${process.env.PLATFORM_NAME}`;
2525

2626
const safeEmail = escapeHtml(email);
2727

@@ -48,8 +48,8 @@ export class CreateEcosystemInviteTemplate {
4848
</p>
4949
5050
<p>
51-
You have been invited to create a new ecosystem
52-
on <strong>${process.env.PLATFORM_NAME}</strong>.
51+
You have been granted access by the platform admin to create a new ecosystem on <strong>${process.env.PLATFORM_NAME}</strong>. ${secondaryMessage}
52+
5353
</p>
5454
5555
<p>${message}</p>
@@ -64,11 +64,6 @@ export class CreateEcosystemInviteTemplate {
6464
text-decoration: none;">
6565
${buttonText}
6666
</a>
67-
68-
<p style="margin-top:10px;">
69-
Verification Link:
70-
<a clicktracking="off" href="${validUrl}">${validUrl}</a>
71-
</p>
7267
</div>
7368
7469
<hr style="border-top:1px solid #e8e8e8" />

libs/prisma-service/prisma/migrations/20260105101757_invitation_to_create_ecosystem/migration.sql renamed to libs/prisma-service/prisma/migrations/20260106131405_send_invitation_to_create_ecosystem/migration.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ CREATE UNIQUE INDEX "ecosystem_users_userId_ecosystemId_key" ON "ecosystem_users
8282
-- CreateIndex
8383
CREATE UNIQUE INDEX "ecosystem_orgs_orgId_ecosystemId_key" ON "ecosystem_orgs"("orgId", "ecosystemId");
8484

85+
-- CreateIndex
86+
CREATE UNIQUE INDEX "ecosystem_invitations_email_key" ON "ecosystem_invitations"("email");
87+
8588
-- AddForeignKey
8689
ALTER TABLE "ecosystem_users" ADD CONSTRAINT "ecosystem_users_ecosystemId_fkey" FOREIGN KEY ("ecosystemId") REFERENCES "ecosystem"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
8790

libs/prisma-service/prisma/schema.prisma

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -800,7 +800,7 @@ model ecosystem_orgs {
800800

801801
model ecosystem_invitations {
802802
id String @id @default(uuid()) @db.Uuid
803-
email String
803+
email String @unique
804804
status String
805805
806806
userId String? @db.Uuid
@@ -810,7 +810,7 @@ model ecosystem_invitations {
810810
lastChangedDateTime DateTime @default(now()) @db.Timestamptz(6)
811811
lastChangedBy String @db.Uuid
812812
813-
user user? @relation(fields: [userId], references: [id])
813+
user user? @relation(fields: [userId], references: [id])
814814
}
815815

816816

0 commit comments

Comments
 (0)