Add editAgreementDetails and acceptHostApplication handlers; update serverless.yml and Prisma schema

This commit is contained in:
2025-11-20 15:23:15 +05:30
parent 13ffee5f7e
commit 6bbcb36b10
5 changed files with 314 additions and 90 deletions

View File

@@ -681,6 +681,8 @@ model HostHeader {
isCommisionBase Boolean @default(false) @map("is_commision_base")
commisionPer Float? @map("commision_per")
amountPerBooking Int? @map("amount_per_booking")
payoutDurationNum Int? @map("payout_duration_num")
payoutDurationFrequency String? @map("payout_duration_frequency")
isActive Boolean @default(true) @map("is_active")
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
@@ -743,8 +745,8 @@ model HostSuggestion {
comments String @map("comments")
isparent Boolean @default(false) @map("is_parent")
isreviewed Boolean @default(false) @map("is_reviewed")
reviewedByXid Int @map("reviewed_by_xid")
reviewedBy User @relation("UserReviewedSuggestions", fields: [reviewedByXid], references: [id], onDelete: Cascade)
reviewedByXid Int? @map("reviewed_by_xid")
reviewedBy User? @relation("UserReviewedSuggestions", fields: [reviewedByXid], references: [id], onDelete: Cascade)
reviewOn DateTime? @map("review_on")
isActive Boolean @default(true) @map("is_active")
createdAt DateTime @default(now()) @map("created_at")

View File

@@ -412,6 +412,21 @@ functions:
path: /minglaradmin/assign-am-to-host
method: patch
editAgreementDetails:
handler: src/modules/minglaradmin/handlers/editAgreementDetails.handler
package:
patterns:
- 'src/modules/minglaradmin/**'
- 'common/**'
- 'src/common/**'
- 'node_modules/@prisma/client/**'
- 'node_modules/.prisma/**'
events:
- httpApi:
path: /minglaradmin/edit-agreement-details
method: patch
addCompanyDetails:
handler: src/modules/host/handlers/addCompanyDetails.handler
package:

View File

@@ -0,0 +1,72 @@
import { APIGatewayProxyEvent, APIGatewayProxyResult, Context } from 'aws-lambda';
import { PrismaService } from '../../../common/database/prisma.service';
import { verifyMinglarAdminToken } from '../../../common/middlewares/jwt/authForMinglarAdmin';
import { safeHandler } from '../../../common/utils/handlers/safeHandler';
import ApiError from '../../../common/utils/helper/ApiError';
import { MinglarService } from '../services/minglar.service';
const prismaService = new PrismaService();
const minglarService = new MinglarService(prismaService);
interface AddSuggestionBody {
hostXid: number;
title: string;
comments: string;
}
/**
* Add suggestion handler for host applications
* Allows Minglar Admin, Co_Admin, and Account Manager to add suggestions
* Types: Setup Profile, Review Account, Add Payment Details, Agreement
*/
export const handler = safeHandler(async (
event: APIGatewayProxyEvent,
context?: Context
): Promise<APIGatewayProxyResult> => {
// Verify authentication token
const token = event.headers['x-auth-token'] || event.headers['X-Auth-Token'];
if (!token) {
throw new ApiError(401, 'This is a protected route. Please provide a valid token.');
}
// Verify token and get user info
const userInfo = await verifyMinglarAdminToken(token);
// Get user details
const user = await prismaService.user.findUnique({
where: { id: userInfo.id },
select: { id: true, roleXid: true }
});
if (!user) {
throw new ApiError(404, 'User not found');
}
// Parse request body
let body: AddSuggestionBody;
try {
body = event.body ? JSON.parse(event.body) : {};
} catch (error) {
throw new ApiError(400, 'Invalid JSON in request body');
}
const { hostXid } = body;
// Add suggestion using service
await minglarService.acceptHostApplication(hostXid, user.id);
return {
statusCode: 200,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
},
body: JSON.stringify({
success: true,
message: 'Application accepted successfully',
data: null,
}),
};
});

View File

@@ -0,0 +1,90 @@
import { verifyOnlyMinglarAdminToken } from '@/common/middlewares/jwt/authForOnlyMinglarAdmin';
import { APIGatewayProxyEvent, APIGatewayProxyResult, Context } from 'aws-lambda';
import { PrismaService } from '../../../common/database/prisma.service';
import { safeHandler } from '../../../common/utils/handlers/safeHandler';
import ApiError from '../../../common/utils/helper/ApiError';
import { MinglarService } from '../services/minglar.service';
const prismaService = new PrismaService();
const minglarService = new MinglarService(prismaService);
interface assignAMToHostBody {
host_xid: number,
agreementStartDate: string,
duration: number,
isCommisionBase: boolean,
commisionPer: number,
amountPerBooking: number,
durationFrequency: string,
payoutDurationNum: number,
payoutDurationFrequency: string
}
export const handler = safeHandler(async (
event: APIGatewayProxyEvent,
context?: Context
): Promise<APIGatewayProxyResult> => {
// Verify authentication token
const token = event.headers['x-auth-token'] || event.headers['X-Auth-Token'];
if (!token) {
throw new ApiError(401, 'This is a protected route. Please provide a valid token.');
}
// Verify token and get user info
const userInfo = await verifyOnlyMinglarAdminToken(token);
// Get user details including role
const user = await prismaService.user.findUnique({
where: { id: userInfo.id },
select: { id: true, roleXid: true }
});
if (!user) {
throw new ApiError(404, 'User not found');
}
// Parse request body
let body: assignAMToHostBody;
try {
body = event.body ? JSON.parse(event.body) : {};
} catch (error) {
throw new ApiError(400, 'Invalid JSON in request body');
}
const {
host_xid,
agreementStartDate,
duration,
isCommisionBase,
commisionPer,
amountPerBooking,
durationFrequency,
payoutDurationNum,
payoutDurationFrequency
} = body;
await minglarService.editAgreementDetails(
host_xid,
agreementStartDate,
duration,
isCommisionBase,
commisionPer,
amountPerBooking,
durationFrequency,
payoutDurationNum,
payoutDurationFrequency
);
return {
statusCode: 200,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
},
body: JSON.stringify({
success: true,
message: 'Details edited successfully',
}),
};
});

View File

@@ -5,7 +5,7 @@ import * as bcrypt from 'bcryptjs';
import { z } from 'zod';
import { hostCompanyDetailsSchema } from '../../../common/utils/validation/host/hostCompanyDetails.validation';
import { CreateMinglarDto, UpdateMinglarDto } from '../dto/minglar.dto';
import { User } from '@prisma/client';
import { HostHeader, User } from '@prisma/client';
import { ROLE } from '@/common/utils/constants/common.constant';
import { MINGLAR_INVITATION_STATUS, MINGLAR_STATUS_DISPLAY, MINGLAR_STATUS_INTERNAL } from '@/common/utils/constants/minglar.constant';
import { HOST_STATUS_DISPLAY, HOST_STATUS_INTERNAL } from '@/common/utils/constants/host.constant';
@@ -539,7 +539,7 @@ export class MinglarService {
async addHostSuggestion(hostXid: number, title: string, comments: string, reviewedByXid: number) {
// Check if host exists
const hostHeader = await this.prisma.hostHeader.findUnique({
where: { userXid: hostXid },
where: { id: hostXid },
select: { id: true }
});
console.log(hostHeader)
@@ -620,6 +620,51 @@ export class MinglarService {
return suggestions;
}
async editAgreementDetails(
host_xid: number,
agreementStartDate: string,
duration: number,
isCommisionBase: boolean,
commisionPer: number,
amountPerBooking: number,
durationFrequency: string,
payoutDurationNum: number,
payoutDurationFrequency: string
) {
return await this.prisma.hostHeader.update({
where: { id: host_xid },
data: {
durationNumber: Number(duration),
durationFrequency: durationFrequency,
agreementStartDate: new Date(agreementStartDate),
isCommisionBase: isCommisionBase,
commisionPer: commisionPer ? Number(commisionPer) : null, // Convert to number if exists
amountPerBooking: amountPerBooking ? Number(amountPerBooking) : null, // Convert to number if exists
payoutDurationNum: Number(payoutDurationNum), // Convert to number
payoutDurationFrequency: payoutDurationFrequency
}
})
}
async acceptHostApplication(host_xid: number, user_xid: number) {
return await this.prisma.hostHeader.update({
where: {
id: host_xid,
hostStatusInternal: HOST_STATUS_INTERNAL.HOST_SUBMITTED,
hostStatusDisplay: HOST_STATUS_DISPLAY.UNDER_REVIEW,
adminStatusInternal: MINGLAR_STATUS_INTERNAL.AM_TO_REVIEW,
adminStatusDisplay: MINGLAR_STATUS_DISPLAY.TO_REVIEW
},
data: {
isApproved: true,
hostStatusInternal: HOST_STATUS_INTERNAL.APPROVED,
hostStatusDisplay: HOST_STATUS_DISPLAY.APPROVED,
adminStatusInternal: MINGLAR_STATUS_INTERNAL.AM_APPROVED,
adminStatusDisplay: MINGLAR_STATUS_DISPLAY.APPROVED
}
})
}
}