Implement parent document deletion and improve host retrieval logic in onboarding process
This commit is contained in:
@@ -426,6 +426,21 @@ export const handler = safeHandler(async (event: APIGatewayProxyEvent): Promise<
|
||||
}
|
||||
}
|
||||
|
||||
if (!parsedCompany.isSubsidairy) {
|
||||
const parentDocuments = await hostService.getParentDocumentsByHostId(userInfo.id);
|
||||
if (parentDocuments.length > 0) {
|
||||
for (const doc of parentDocuments) {
|
||||
try {
|
||||
const s3Key = getS3KeyFromUrl(doc.filePath);
|
||||
await deleteFromS3(s3Key);
|
||||
} catch (e) {
|
||||
console.error("S3 delete failed:", doc.filePath, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
await hostService.deleteExistingParentRecords(userInfo.id)
|
||||
}
|
||||
|
||||
/** 12) SAVE / UPDATE HOST ENTRY */
|
||||
const createdOrUpdated = await hostService.addOrUpdateCompanyDetails(
|
||||
userInfo.id,
|
||||
|
||||
@@ -27,10 +27,6 @@ export const handler = safeHandler(async (
|
||||
// Fetch user with their HostHeader stepper info
|
||||
const host = await hostService.getHostIdByUserXid(userId);
|
||||
|
||||
if (!host) {
|
||||
throw new ApiError(404, 'Host record not found');
|
||||
}
|
||||
|
||||
return {
|
||||
statusCode: 200,
|
||||
headers: {
|
||||
@@ -41,7 +37,7 @@ export const handler = safeHandler(async (
|
||||
success: true,
|
||||
message: 'Stepper information retrieved successfully',
|
||||
data: {
|
||||
stepper: host.stepper,
|
||||
stepper: host?.host?.stepper || null,
|
||||
emailAddress: host.user?.emailAddress || null,
|
||||
},
|
||||
}),
|
||||
|
||||
@@ -85,9 +85,14 @@ export class HostService {
|
||||
async getHostIdByUserXid(user_xid: number) {
|
||||
const host = await this.prisma.hostHeader.findFirst({
|
||||
where: { userXid: user_xid },
|
||||
select: { id: true, companyName: true, countryXid: true, stepper: true, user: { select: { id: true, emailAddress: true }} },
|
||||
select: { id: true, stepper: true },
|
||||
});
|
||||
return host;
|
||||
|
||||
const user = await this.prisma.user.findUnique({
|
||||
where: { id: user_xid },
|
||||
select: { id: true, emailAddress: true },
|
||||
})
|
||||
return { host, user };
|
||||
}
|
||||
|
||||
async getHostById(id: number) {
|
||||
@@ -624,6 +629,52 @@ export class HostService {
|
||||
});
|
||||
}
|
||||
|
||||
async getParentDocumentsByHostId(userId: number) {
|
||||
const host = await this.prisma.hostHeader.findFirst({
|
||||
where: { userXid: userId },
|
||||
select: { id: true },
|
||||
});
|
||||
|
||||
if (!host) return [];
|
||||
|
||||
const parents = await this.prisma.hostParent.findMany({
|
||||
where: { hostXid: host.id },
|
||||
include: { HostParenetDocuments: true },
|
||||
});
|
||||
|
||||
return parents.flatMap(p => p.HostParenetDocuments);
|
||||
}
|
||||
|
||||
|
||||
async deleteExistingParentRecords(userId: number) {
|
||||
const host = await this.prisma.hostHeader.findFirst({
|
||||
where: { userXid: userId },
|
||||
select: { id: true },
|
||||
});
|
||||
|
||||
if (!host) return;
|
||||
|
||||
const parents = await this.prisma.hostParent.findMany({
|
||||
where: { hostXid: host.id },
|
||||
select: { id: true },
|
||||
});
|
||||
|
||||
if (!parents.length) return;
|
||||
|
||||
const parentIds = parents.map(p => p.id);
|
||||
|
||||
// 1️⃣ Delete documents first
|
||||
await this.prisma.hostParenetDocuments.deleteMany({
|
||||
where: { hostParentXid: { in: parentIds } },
|
||||
});
|
||||
|
||||
// 2️⃣ Then delete parent records
|
||||
await this.prisma.hostParent.deleteMany({
|
||||
where: { id: { in: parentIds } },
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
async addOrUpdateCompanyDetails(
|
||||
user_xid: number,
|
||||
companyData: HostCompanyDetailsInput,
|
||||
@@ -775,22 +826,20 @@ export class HostService {
|
||||
const createdParent = await tx.hostParent.create({
|
||||
data: {
|
||||
host: { connect: { id: createdHost.id } },
|
||||
companyName: parentCompanyData.companyName,
|
||||
companyName: parentCompanyData.companyName || null,
|
||||
address1: parentCompanyData.address1 || null,
|
||||
address2: parentCompanyData.address2 || null,
|
||||
// Safely handle city connection - only connect if valid ID exists
|
||||
cities: companyData.cityXid && !isNaN(Number(companyData.cityXid))
|
||||
? { connect: { id: Number(companyData.cityXid) } }
|
||||
: undefined, // Don't change if not provided
|
||||
|
||||
// Same for state
|
||||
states: companyData.stateXid && !isNaN(Number(companyData.stateXid))
|
||||
? { connect: { id: Number(companyData.stateXid) } }
|
||||
cities: parentCompanyData?.cityXid && !isNaN(Number(parentCompanyData.cityXid))
|
||||
? { connect: { id: Number(parentCompanyData.cityXid) } }
|
||||
: undefined,
|
||||
|
||||
// Same for country
|
||||
countries: companyData.countryXid && !isNaN(Number(companyData.countryXid))
|
||||
? { connect: { id: Number(companyData.countryXid) } }
|
||||
states: parentCompanyData?.stateXid && !isNaN(Number(parentCompanyData.stateXid))
|
||||
? { connect: { id: Number(parentCompanyData.stateXid) } }
|
||||
: undefined,
|
||||
|
||||
countries: parentCompanyData?.countryXid && !isNaN(Number(parentCompanyData.countryXid))
|
||||
? { connect: { id: Number(parentCompanyData.countryXid) } }
|
||||
: undefined,
|
||||
pinCode: parentCompanyData.pinCode || null,
|
||||
logoPath: parentCompanyData.logoPath || null,
|
||||
@@ -927,17 +976,19 @@ export class HostService {
|
||||
const createdParent = await tx.hostParent.create({
|
||||
data: {
|
||||
host: { connect: { id: updatedHost.id } },
|
||||
companyName: parentCompanyData.companyName,
|
||||
companyName: parentCompanyData.companyName || null,
|
||||
address1: parentCompanyData.address1 || null,
|
||||
address2: parentCompanyData.address2 || null,
|
||||
cities: parentCompanyData.cityXid
|
||||
? { connect: { id: parentCompanyData.cityXid } }
|
||||
cities: parentCompanyData?.cityXid && !isNaN(Number(parentCompanyData.cityXid))
|
||||
? { connect: { id: Number(parentCompanyData.cityXid) } }
|
||||
: undefined,
|
||||
states: parentCompanyData.stateXid
|
||||
? { connect: { id: parentCompanyData.stateXid } }
|
||||
|
||||
states: parentCompanyData?.stateXid && !isNaN(Number(parentCompanyData.stateXid))
|
||||
? { connect: { id: Number(parentCompanyData.stateXid) } }
|
||||
: undefined,
|
||||
countries: parentCompanyData.countryXid
|
||||
? { connect: { id: parentCompanyData.countryXid } }
|
||||
|
||||
countries: parentCompanyData?.countryXid && !isNaN(Number(parentCompanyData.countryXid))
|
||||
? { connect: { id: Number(parentCompanyData.countryXid) } }
|
||||
: undefined,
|
||||
pinCode: parentCompanyData.pinCode || null,
|
||||
logoPath: parentCompanyData.logoPath || existingParentCompany.logoPath,
|
||||
@@ -974,17 +1025,19 @@ export class HostService {
|
||||
await tx.hostParent.update({
|
||||
where: { id: parentRecord.id },
|
||||
data: {
|
||||
companyName: parentCompanyData.companyName,
|
||||
companyName: parentCompanyData.companyName || null,
|
||||
address1: parentCompanyData.address1 || null,
|
||||
address2: parentCompanyData.address2 || null,
|
||||
cities: parentCompanyData.cityXid
|
||||
? { connect: { id: parentCompanyData.cityXid } }
|
||||
cities: parentCompanyData?.cityXid && !isNaN(Number(parentCompanyData.cityXid))
|
||||
? { connect: { id: Number(parentCompanyData.cityXid) } }
|
||||
: undefined,
|
||||
states: parentCompanyData.stateXid
|
||||
? { connect: { id: parentCompanyData.stateXid } }
|
||||
|
||||
states: parentCompanyData?.stateXid && !isNaN(Number(parentCompanyData.stateXid))
|
||||
? { connect: { id: Number(parentCompanyData.stateXid) } }
|
||||
: undefined,
|
||||
countries: parentCompanyData.countryXid
|
||||
? { connect: { id: parentCompanyData.countryXid } }
|
||||
|
||||
countries: parentCompanyData?.countryXid && !isNaN(Number(parentCompanyData.countryXid))
|
||||
? { connect: { id: Number(parentCompanyData.countryXid) } }
|
||||
: undefined,
|
||||
pinCode: parentCompanyData.pinCode || null,
|
||||
logoPath: parentCompanyData.logoPath || existingParentCompany.logoPath,
|
||||
|
||||
Reference in New Issue
Block a user