Add S3 document deletion functionality in submitCompanyDetails handler; update host service to return activity ID with sorted categories; modify PQP details retrieval to use new admin host token for authentication.

This commit is contained in:
2025-12-04 18:40:41 +05:30
parent 759eeb298c
commit d898dcd8ff
4 changed files with 95 additions and 3 deletions

View File

@@ -50,6 +50,25 @@ function cleanEmptyStrings(obj: any) {
return cleaned;
}
function getS3KeyFromUrl(url: string): string {
const base = `https://${config.aws.bucketName}.s3.${config.aws.region}.amazonaws.com/`;
return url.replace(base, "");
}
async function deleteFromS3(key: string) {
try {
await s3.deleteObject({
Bucket: config.aws.bucketName,
Key: key
}).promise();
console.log("✅ Deleted from S3:", key);
} catch (err) {
console.error("❌ Failed to delete from S3:", key, err);
}
}
export const handler = safeHandler(async (event: APIGatewayProxyEvent): Promise<APIGatewayProxyResult> => {
try {
@@ -112,6 +131,9 @@ export const handler = safeHandler(async (event: APIGatewayProxyEvent): Promise<
bb.end();
});
const deletedFiles = normalizeJsonField(fields, "deletedFiles") || [];
const parentDeletedFiles = normalizeJsonField(fields, "parentDeletedFiles") || [];
/** 4) Extract and clean isDraft flag */
const isDraft = fields.isDraft === 'true' || fields.isDraft === true;
@@ -208,6 +230,63 @@ export const handler = safeHandler(async (event: APIGatewayProxyEvent): Promise<
parsedParentCompany = parsedCompany.parentCompany || null;
}
/** 9.5) DELETE DOCUMENTS IF REQUESTED **/
if (Array.isArray(deletedFiles) && deletedFiles.length > 0) {
console.log(`🗑️ Deleting ${deletedFiles.length} documents...`);
for (const del of deletedFiles) {
const id = Number(del.id);
const url = del.url;
if (!id || !url) {
console.log("❌ Invalid delete entry:", del);
continue;
}
// Extract S3 key
const s3Key = getS3KeyFromUrl(url);
// Delete from S3
await deleteFromS3(s3Key);
// Delete from DB
await prisma.hostDocuments.delete({
where: { id }
});
console.log(`🗑️ Deleted host document ID ${id}`);
}
}
/** 9.6) DELETE PARENT DOCUMENTS **/
if (parsedCompany.isSubsidairy && Array.isArray(parentDeletedFiles) && parentDeletedFiles.length > 0) {
console.log(`🗑️ Deleting ${parentDeletedFiles.length} PARENT documents...`);
for (const del of parentDeletedFiles) {
const id = Number(del.id);
const url = del.url;
if (!id || !url) {
console.log("⚠️ Invalid parent delete entry:", del);
continue;
}
const s3Key = getS3KeyFromUrl(url);
// Delete S3
await deleteFromS3(s3Key);
// Delete DB
await prisma.hostParenetDocuments.delete({
where: { id }
});
console.log(`🗑️ Deleted PARENT document ID ${id}`);
}
}
/** 11) UPLOAD DOCUMENTS */
async function uploadToS3(buffer, mimeType, originalName, folderType, documentTypeXid?, fieldName?) {
const ext = originalName.split('.').pop() || 'jpg';

View File

@@ -1737,7 +1737,10 @@ export class HostService {
}
}
return sortedCategories;
return {
activity_xid: created.id,
sortedCategories
};
});
}

View File

@@ -1,9 +1,9 @@
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 { APIGatewayProxyEvent, APIGatewayProxyResult, Context } from 'aws-lambda';
import { MinglarService } from '../../../services/minglar.service';
import { verifyMinglarAdminHostToken } from '../../../../../common/middlewares/jwt/authForMinglarAdminHost';
const prismaService = new PrismaService();
const minglarService = new MinglarService(prismaService);
@@ -18,7 +18,7 @@ export const handler = safeHandler(async (
throw new ApiError(400, 'This is a protected route. Please provide a valid token.');
}
await verifyMinglarAdminToken(token);
await verifyMinglarAdminHostToken(token);
const activityXid = event.pathParameters?.activityXid;
if (!activityXid) {

View File

@@ -291,6 +291,16 @@ export class MinglarService {
amInternalStatus: true,
amDisplayStatus: true,
createdAt: true,
host: {
select: {
companyName: true,
user: {
select: {
userRefNumber: true
}
}
}
},
ActivityAmDetails: {
select: {
accountManager: {