made s3 uploader and apis
This commit is contained in:
@@ -72,4 +72,22 @@ export class GetHostLoginResponseDTO {
|
||||
this.accessToken = accessToken;
|
||||
this.refreshToken = refreshToken;
|
||||
}
|
||||
}
|
||||
|
||||
export class AddPaymentDetailsDTO {
|
||||
bankXid: number;
|
||||
bankBranchXid: number;
|
||||
accountNumber: number;
|
||||
accountHolderName: string;
|
||||
ifscCode: string;
|
||||
hostXid: number;
|
||||
|
||||
constructor(bankXid: number, bankBranchXid: number, accountNumber: number, accountHolderName: string, ifscCode: string, hostXid: number) {
|
||||
this.bankXid = bankXid;
|
||||
this.bankBranchXid = bankBranchXid;
|
||||
this.accountNumber = accountNumber;
|
||||
this.accountHolderName = accountHolderName;
|
||||
this.ifscCode = ifscCode;
|
||||
this.hostXid = hostXid;
|
||||
}
|
||||
}
|
||||
65
src/modules/host/handlers/addPaymentDetails.ts
Normal file
65
src/modules/host/handlers/addPaymentDetails.ts
Normal file
@@ -0,0 +1,65 @@
|
||||
import { APIGatewayProxyEvent, APIGatewayProxyResult, Context } from 'aws-lambda';
|
||||
import { safeHandler } from '../../../common/utils/handlers/safeHandler';
|
||||
import { PrismaService } from '../../../common/database/prisma.service';
|
||||
import { HostService } from '../services/host.service';
|
||||
import ApiError from '../../../common/utils/helper/ApiError';
|
||||
import { verifyHostToken } from '@/common/middlewares/jwt/authForHost';
|
||||
import { hostBankDetailsSchema } from '@/common/utils/validation/host/addPaymentDetails.validation';
|
||||
|
||||
const prismaService = new PrismaService();
|
||||
const hostService = new HostService(prismaService);
|
||||
|
||||
export const handler = safeHandler(async (
|
||||
event: APIGatewayProxyEvent,
|
||||
context?: Context
|
||||
): Promise<APIGatewayProxyResult> => {
|
||||
// Extract token from headers
|
||||
const token = event.headers['x-auth-token'] || event.headers['X-Auth-Token']
|
||||
if (!token) {
|
||||
throw new ApiError(400, 'This is a protected route. Please provide a valid token.');
|
||||
}
|
||||
|
||||
// Authenticate user using the shared authForHost function
|
||||
const userInfo = await verifyHostToken(token);
|
||||
const hostId = userInfo.id;
|
||||
|
||||
if (Number.isNaN(hostId)) {
|
||||
throw new ApiError(400, 'Host id must be a number');
|
||||
}
|
||||
|
||||
// Parse request body
|
||||
let body: { bankXid?: number; bankBranchXid?: number; accountNumber?: string; accountHolderName?: string; ifscCode?: string };
|
||||
|
||||
try {
|
||||
body = event.body ? JSON.parse(event.body) : {};
|
||||
} catch (error) {
|
||||
throw new ApiError(400, 'Invalid JSON in request body');
|
||||
}
|
||||
|
||||
// ✅ Validate payload using Zod
|
||||
const validationResult = hostBankDetailsSchema.safeParse({
|
||||
...(body as object),
|
||||
hostXid: hostId, // inject hostId from token (not from user input)
|
||||
});
|
||||
|
||||
if (!validationResult.success) {
|
||||
const errorMessages = validationResult.error.issues.map(e => e.message).join(', ');
|
||||
throw new ApiError(400, `Validation failed: ${errorMessages}`);
|
||||
}
|
||||
|
||||
const validatedData = validationResult.data;
|
||||
|
||||
await hostService.addPaymentDetails(hostId, validatedData);
|
||||
|
||||
return {
|
||||
statusCode: 200,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
success: true,
|
||||
message: 'Payment details added successfully',
|
||||
}),
|
||||
};
|
||||
});
|
||||
@@ -1,7 +1,7 @@
|
||||
// src/modules/host/services/host.service.ts
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { PrismaService } from '../../../common/database/prisma.service';
|
||||
import { CreateHostDto, UpdateHostDto } from '../dto/host.dto';
|
||||
import { AddPaymentDetailsDTO, CreateHostDto, UpdateHostDto } from '../dto/host.dto';
|
||||
import * as bcrypt from 'bcryptjs';
|
||||
import ApiError from '../../../common/utils/helper/ApiError';
|
||||
import { User } from '@prisma/client';
|
||||
@@ -143,4 +143,24 @@ export class HostService {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
async addPaymentDetails(id: number, data: AddPaymentDetailsDTO): Promise<AddPaymentDetailsDTO> {
|
||||
const existingUser = await this.prisma.user.findUnique({
|
||||
where: { id },
|
||||
});
|
||||
|
||||
if (!existingUser) {
|
||||
throw new ApiError(404, 'User not found');
|
||||
}
|
||||
|
||||
const addedPaymentDetails = await this.prisma.hostBankDetails.create({
|
||||
data,
|
||||
});
|
||||
|
||||
if (!addedPaymentDetails) {
|
||||
throw new ApiError(400, 'Failed to add payment details');
|
||||
}
|
||||
|
||||
return addedPaymentDetails;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user