Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Api response #345

Merged
merged 6 commits into from
Dec 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 23 additions & 23 deletions apps/api-gateway/src/organization/organization.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ export class OrganizationController {
@Get('/profile/:orgId')
@ApiOperation({ summary: 'Organization Profile', description: 'Update an organization' })
@ApiResponse({ status: 200, description: 'Success', type: ApiResponseDto })
async getOgPofile(@Param('orgId') orgId: string, @Res() res: Response): Promise<Response> {
async getOgPofile(@Param('orgId') orgId: string, @Res() res: Response): Promise<IResponseType> {
const orgProfile = await this.organizationService.getOgPofile(orgId);

const base64Data = orgProfile.response["logoUrl"];
const base64Data = orgProfile['logoUrl'];
const getImageBuffer = await this.imageServiceService.getBase64Image(base64Data);

res.setHeader('Content-Type', 'image/png');
Expand Down Expand Up @@ -76,13 +76,13 @@ export class OrganizationController {
type: String,
required: false
})
async get(@Query() getAllUsersDto: GetAllOrganizationsDto, @Res() res: Response): Promise<Response> {
async get(@Query() getAllUsersDto: GetAllOrganizationsDto, @Res() res: Response): Promise<IResponseType> {

const users = await this.organizationService.getPublicOrganizations(getAllUsersDto);
const finalResponse: IResponseType = {
statusCode: HttpStatus.OK,
message: ResponseMessages.organisation.success.getOrganizations,
data: users.response
data: users
};

return res.status(HttpStatus.OK).json(finalResponse);
Expand All @@ -96,7 +96,7 @@ export class OrganizationController {
@ApiResponse({ status: 200, description: 'Success', type: ApiResponseDto })
@UseGuards(AuthGuard('jwt'))
@ApiBearerAuth()
async getOrgRoles(@Res() res: Response): Promise<Response> {
async getOrgRoles(@Res() res: Response): Promise<IResponseType> {

const orgRoles = await this.organizationService.getOrgRoles();

Expand All @@ -119,15 +119,15 @@ export class OrganizationController {
@ApiParam({
name: 'orgSlug',
type: String,
required: false
required: true
})
async getPublicProfile(@Param('orgSlug') orgSlug: string, @Res() res: Response): Promise<object> {
async getPublicProfile(@Param('orgSlug') orgSlug: string, @Res() res: Response): Promise<IResponseType> {
const userData = await this.organizationService.getPublicProfile(orgSlug);

const finalResponse: IResponseType = {
statusCode: HttpStatus.OK,
message: ResponseMessages.organisation.success.fetchProfile,
data: userData.response
data: userData
};

return res.status(HttpStatus.OK).json(finalResponse);
Expand All @@ -141,14 +141,14 @@ export class OrganizationController {
@UseGuards(AuthGuard('jwt'))
@ApiBearerAuth()

async getOrganizationDashboard(@Param('orgId') orgId: string, @Res() res: Response, @User() reqUser: user): Promise<Response> {
async getOrganizationDashboard(@Param('orgId') orgId: string, @Res() res: Response, @User() reqUser: user): Promise<IResponseType> {

const getOrganization = await this.organizationService.getOrganizationDashboard(orgId, reqUser.id);

const finalResponse: IResponseType = {
statusCode: HttpStatus.OK,
message: ResponseMessages.organisation.success.getOrgDashboard,
data: getOrganization.response
data: getOrganization
};
return res.status(HttpStatus.OK).json(finalResponse);

Expand All @@ -175,14 +175,14 @@ export class OrganizationController {
required: false
})
@Roles(OrgRoles.OWNER, OrgRoles.SUPER_ADMIN, OrgRoles.ADMIN, OrgRoles.ISSUER, OrgRoles.VERIFIER, OrgRoles.MEMBER)
async getInvitationsByOrgId(@Param('orgId') orgId: string, @Query() getAllInvitationsDto: GetAllSentInvitationsDto, @Res() res: Response): Promise<Response> {
async getInvitationsByOrgId(@Param('orgId') orgId: string, @Query() getAllInvitationsDto: GetAllSentInvitationsDto, @Res() res: Response): Promise<IResponseType> {

const getInvitationById = await this.organizationService.getInvitationsByOrgId(orgId, getAllInvitationsDto);

const finalResponse: IResponseType = {
statusCode: HttpStatus.OK,
message: ResponseMessages.organisation.success.getInvitation,
data: getInvitationById.response
data: getInvitationById
};
return res.status(HttpStatus.OK).json(finalResponse);

Expand Down Expand Up @@ -211,14 +211,14 @@ export class OrganizationController {
type: String,
required: false
})
async getOrganizations(@Query() getAllOrgsDto: GetAllOrganizationsDto, @Res() res: Response, @User() reqUser: user): Promise<Response> {
async getOrganizations(@Query() getAllOrgsDto: GetAllOrganizationsDto, @Res() res: Response, @User() reqUser: user): Promise<IResponseType> {

const getOrganizations = await this.organizationService.getOrganizations(getAllOrgsDto, reqUser.id);

const finalResponse: IResponseType = {
statusCode: HttpStatus.OK,
message: ResponseMessages.organisation.success.getOrganizations,
data: getOrganizations.response
data: getOrganizations
};
return res.status(HttpStatus.OK).json(finalResponse);

Expand All @@ -230,14 +230,14 @@ export class OrganizationController {
@UseGuards(AuthGuard('jwt'), OrgRolesGuard)
@ApiBearerAuth()
@Roles(OrgRoles.OWNER, OrgRoles.ADMIN, OrgRoles.ISSUER, OrgRoles.VERIFIER, OrgRoles.MEMBER)
async getOrganization(@Param('orgId') orgId: string, @Res() res: Response, @User() reqUser: user): Promise<Response> {
async getOrganization(@Param('orgId') orgId: string, @Res() res: Response, @User() reqUser: user): Promise<IResponseType> {

const getOrganization = await this.organizationService.getOrganization(orgId, reqUser.id);

const finalResponse: IResponseType = {
statusCode: HttpStatus.OK,
message: ResponseMessages.organisation.success.getOrganization,
data: getOrganization.response
data: getOrganization
};
return res.status(HttpStatus.OK).json(finalResponse);

Expand Down Expand Up @@ -271,12 +271,12 @@ export class OrganizationController {
type: String,
required: false
})
async getOrganizationUsers(@User() user: IUserRequestInterface, @Query() getAllUsersDto: GetAllUsersDto, @Param('orgId') orgId: string, @Res() res: Response): Promise<Response> {
async getOrganizationUsers(@User() user: IUserRequestInterface, @Query() getAllUsersDto: GetAllUsersDto, @Param('orgId') orgId: string, @Res() res: Response): Promise<IResponseType> {
const users = await this.organizationService.getOrgUsers(orgId, getAllUsersDto);
const finalResponse: IResponseType = {
statusCode: HttpStatus.OK,
message: ResponseMessages.user.success.fetchUsers,
data: users?.response
data: users
};

return res.status(HttpStatus.OK).json(finalResponse);
Expand All @@ -287,7 +287,7 @@ export class OrganizationController {
@ApiResponse({ status: 201, description: 'Success', type: ApiResponseDto })
@UseGuards(AuthGuard('jwt'))
@ApiBearerAuth()
async createOrganization(@Body() createOrgDto: CreateOrganizationDto, @Res() res: Response, @User() reqUser: user): Promise<Response> {
async createOrganization(@Body() createOrgDto: CreateOrganizationDto, @Res() res: Response, @User() reqUser: user): Promise<IResponseType> {
await this.organizationService.createOrganization(createOrgDto, reqUser.id);
const finalResponse: IResponseType = {
statusCode: HttpStatus.CREATED,
Expand All @@ -305,7 +305,7 @@ export class OrganizationController {
@Roles(OrgRoles.OWNER, OrgRoles.SUPER_ADMIN, OrgRoles.ADMIN)
@UseGuards(AuthGuard('jwt'), OrgRolesGuard)
@ApiBearerAuth()
async createInvitation(@Body() bulkInvitationDto: BulkSendInvitationDto, @Param('orgId') orgId: string, @User() user: user, @Res() res: Response): Promise<Response> {
async createInvitation(@Body() bulkInvitationDto: BulkSendInvitationDto, @Param('orgId') orgId: string, @User() user: user, @Res() res: Response): Promise<IResponseType> {

bulkInvitationDto.orgId = orgId;
await this.organizationService.createInvitation(bulkInvitationDto, user.id, user.email);
Expand All @@ -325,7 +325,7 @@ export class OrganizationController {
@UseGuards(AuthGuard('jwt'), OrgRolesGuard)
@ApiResponse({ status: 200, description: 'Success', type: ApiResponseDto })
@ApiOperation({ summary: 'Update user roles', description: 'update user roles' })
async updateUserRoles(@Body() updateUserDto: UpdateUserRolesDto, @Param('orgId') orgId: string, @Param('userId') userId: string, @Res() res: Response): Promise<Response> {
async updateUserRoles(@Body() updateUserDto: UpdateUserRolesDto, @Param('orgId') orgId: string, @Param('userId') userId: string, @Res() res: Response): Promise<IResponseType> {

updateUserDto.orgId = orgId;
updateUserDto.userId = userId;
Expand All @@ -345,7 +345,7 @@ export class OrganizationController {
@ApiBearerAuth()
@Roles(OrgRoles.OWNER, OrgRoles.ADMIN)
@UseGuards(AuthGuard('jwt'), OrgRolesGuard)
async updateOrganization(@Body() updateOrgDto: UpdateOrganizationDto, @Param('orgId') orgId: string, @Res() res: Response, @User() reqUser: user): Promise<Response> {
async updateOrganization(@Body() updateOrgDto: UpdateOrganizationDto, @Param('orgId') orgId: string, @Res() res: Response, @User() reqUser: user): Promise<IResponseType> {

updateOrgDto.orgId = orgId;
await this.organizationService.updateOrganization(updateOrgDto, reqUser.id, orgId);
Expand All @@ -362,7 +362,7 @@ export class OrganizationController {
@ApiResponse({ status: 200, description: 'Success', type: ApiResponseDto })
@ApiBearerAuth()
@UseGuards(AuthGuard('jwt'))
async deleteOrganization(@Param('orgId') orgId: number, @Res() res: Response): Promise<Response> {
async deleteOrganization(@Param('orgId') orgId: number, @Res() res: Response): Promise<IResponseType> {

await this.organizationService.deleteOrganization(orgId);

Expand Down
39 changes: 23 additions & 16 deletions apps/api-gateway/src/organization/organization.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import { BulkSendInvitationDto } from './dtos/send-invitation.dto';
import { UpdateUserRolesDto } from './dtos/update-user-roles.dto';
import { UpdateOrganizationDto } from './dtos/update-organization-dto';
import { GetAllUsersDto } from '../user/dto/get-all-users.dto';
import { IOrgRoles } from 'libs/org-roles/interfaces/org-roles.interface';
import { organisation } from '@prisma/client';
import { IGetOrgById, IGetOrgs, IOrgInvitationsPagination, IOrganizationDashboard } from 'apps/organization/interfaces/organization.interface';
import { IOrgUsers } from 'apps/user/interfaces/user.interface';

@Injectable()
export class OrganizationService extends BaseService {
Expand All @@ -21,7 +25,7 @@ export class OrganizationService extends BaseService {
* @param createOrgDto
* @returns Organization creation Success
*/
async createOrganization(createOrgDto: CreateOrganizationDto, userId: string): Promise<object> {
async createOrganization(createOrgDto: CreateOrganizationDto, userId: string): Promise<organisation> {
const payload = { createOrgDto, userId };
return this.sendNats(this.serviceProxy, 'create-organization', payload);
}
Expand All @@ -31,7 +35,7 @@ export class OrganizationService extends BaseService {
* @param updateOrgDto
* @returns Organization update Success
*/
async updateOrganization(updateOrgDto: UpdateOrganizationDto, userId: string, orgId: string): Promise<object> {
async updateOrganization(updateOrgDto: UpdateOrganizationDto, userId: string, orgId: string): Promise<organisation> {
const payload = { updateOrgDto, userId, orgId };
return this.sendNats(this.serviceProxy, 'update-organization', payload);
}
Expand All @@ -41,22 +45,24 @@ export class OrganizationService extends BaseService {
* @param
* @returns Organizations details
*/
async getOrganizations(getAllOrgsDto: GetAllOrganizationsDto, userId: string): Promise<{ response: object }> {

async getOrganizations(getAllOrgsDto: GetAllOrganizationsDto, userId: string): Promise<IGetOrgs> {
const payload = { userId, ...getAllOrgsDto };
return this.sendNats(this.serviceProxy, 'get-organizations', payload);
const fetchOrgs = await this.sendNats(this.serviceProxy, 'get-organizations', payload);
return fetchOrgs;
}

/**
*
* @param
* @returns Public organizations list
*/
async getPublicOrganizations(getAllOrgsDto: GetAllOrganizationsDto): Promise<{ response: object }> {
async getPublicOrganizations(getAllOrgsDto: GetAllOrganizationsDto): Promise<IGetOrgs> {
const payload = { ...getAllOrgsDto };
return this.sendNats(this.serviceProxy, 'get-public-organizations', payload);
}

async getPublicProfile(orgSlug: string): Promise<{ response: object }> {
async getPublicProfile(orgSlug: string): Promise<IGetOrgById> {
const payload = { orgSlug };
try {
return this.sendNats(this.serviceProxy, 'get-organization-public-profile', payload);
Expand All @@ -70,7 +76,7 @@ export class OrganizationService extends BaseService {
* @param orgId
* @returns Organization get Success
*/
async getOrganization(orgId: string, userId: string): Promise<{ response: object }> {
async getOrganization(orgId: string, userId: string): Promise<IGetOrgById> {
const payload = { orgId, userId };
return this.sendNats(this.serviceProxy, 'get-organization-by-id', payload);
}
Expand All @@ -83,13 +89,13 @@ export class OrganizationService extends BaseService {
async getInvitationsByOrgId(
orgId: string,
getAllInvitationsDto: GetAllSentInvitationsDto
): Promise<{ response: object }> {
): Promise<IOrgInvitationsPagination> {
const { pageNumber, pageSize, search } = getAllInvitationsDto;
const payload = { orgId, pageNumber, pageSize, search };
return this.sendNats(this.serviceProxy, 'get-invitations-by-orgId', payload);
}

async getOrganizationDashboard(orgId: string, userId: string): Promise<{ response: object }> {
async getOrganizationDashboard(orgId: string, userId: string): Promise<IOrganizationDashboard> {
const payload = { orgId, userId };
return this.sendNats(this.serviceProxy, 'get-organization-dashboard', payload);
}
Expand All @@ -99,7 +105,8 @@ export class OrganizationService extends BaseService {
* @param
* @returns get organization roles
*/
async getOrgRoles(): Promise<object> {

async getOrgRoles(): Promise<IOrgRoles[]> {
const payload = {};
return this.sendNats(this.serviceProxy, 'get-org-roles', payload);
}
Expand All @@ -109,7 +116,7 @@ export class OrganizationService extends BaseService {
* @param sendInvitationDto
* @returns Organization invitation creation Success
*/
async createInvitation(bulkInvitationDto: BulkSendInvitationDto, userId: string, userEmail: string): Promise<object> {
async createInvitation(bulkInvitationDto: BulkSendInvitationDto, userId: string, userEmail: string): Promise<string> {
const payload = { bulkInvitationDto, userId, userEmail };
return this.sendNats(this.serviceProxy, 'send-invitation', payload);
}
Expand All @@ -120,15 +127,15 @@ export class OrganizationService extends BaseService {
* @param userId
* @returns User roles update response
*/
async updateUserRoles(updateUserDto: UpdateUserRolesDto, userId: string): Promise<{ response: boolean }> {
async updateUserRoles(updateUserDto: UpdateUserRolesDto, userId: string): Promise<boolean> {
const payload = { orgId: updateUserDto.orgId, roleIds: updateUserDto.orgRoleId, userId };
return this.sendNats(this.serviceProxy, 'update-user-roles', payload);
}

async getOrgUsers(
orgId: string,
getAllUsersDto: GetAllUsersDto
): Promise<{ response: object }> {
): Promise<IOrgUsers> {
const { pageNumber, pageSize, search } = getAllUsersDto;
const payload = { orgId, pageNumber, pageSize, search };

Expand All @@ -137,15 +144,15 @@ export class OrganizationService extends BaseService {

async getOgPofile(
orgId: string
): Promise<{ response: object }> {
): Promise<organisation> {
const payload = { orgId };

return this.sendNats(this.serviceProxy, 'fetch-organization-profile', payload);
}

async deleteOrganization(
orgId: number
): Promise<{ response: object }> {
): Promise<boolean> {
const payload = { orgId };

return this.sendNats(this.serviceProxy, 'delete-organization', payload);
Expand Down
Loading