fix: fix several different data parsing issues

This commit is contained in:
2026-04-18 14:47:06 +00:00
parent 5141ed20f9
commit f91d8debcb
16 changed files with 632 additions and 49 deletions
+2
View File
@@ -18,6 +18,7 @@ export default createRoute(
const includePrimaryContact =
c.req.query("includePrimaryContact") === "true";
const includeAllContacts = c.req.query("includeAllContacts") === "true";
const includeAllAddresses = c.req.query("includeAllAddresses") === "true";
console.log(company.toJson({ includeAddress, includePrimaryContact, includeAllContacts }));
@@ -49,6 +50,7 @@ export default createRoute(
includeAddress,
includePrimaryContact,
includeAllContacts,
includeAllAddresses,
});
const gatedData = await processObjectValuePerms(
companyData,
@@ -118,6 +118,7 @@ export default createRoute(
);
}
console.error("[Opportunity Create] DB write failed after CW create:", err);
throw new GenericError({
status: 500,
name: "OpportunityCreateError",
+39 -8
View File
@@ -288,19 +288,50 @@ export class CompanyController {
includeAddress?: boolean;
includePrimaryContact?: boolean;
includeAllContacts?: boolean;
includeAllAddresses?: boolean;
}) {
const cw_Data: Record<string, unknown> = {};
if (opts?.includeAddress && this.cw_Data) {
const addr = this.cw_Data.company;
cw_Data.address = {
line1: addr.addressLine1 ?? null,
line2: addr.addressLine2 ?? null,
if (opts?.includeAddress) {
if (this.cw_Data) {
const addr = this.cw_Data.company;
cw_Data.address = {
line1: addr.addressLine1 ?? null,
line2: addr.addressLine2 ?? null,
city: addr.city ?? null,
state: addr.state ?? null,
zip: addr.zip ?? null,
country: addr.country?.name ?? "United States",
};
} else if (this._defaultAddress) {
const addr = this._defaultAddress;
cw_Data.address = {
line1: addr.addressLine1 ?? null,
line2: addr.addressLine2 ?? null,
city: addr.city ?? null,
state: addr.state ?? null,
zip: addr.zipCode ?? null,
country: addr.country ?? "United States",
};
}
}
if (opts?.includeAllAddresses) {
cw_Data.allAddresses = this._addresses.map((addr) => ({
id: addr.id,
uid: addr.uid,
name: addr.name,
description: addr.description ?? null,
defaultFlag: addr.defaultFlag,
inactiveFlag: addr.inactiveFlag,
addressLine1: addr.addressLine1 ?? null,
addressLine2: addr.addressLine2 ?? null,
city: addr.city ?? null,
state: addr.state ?? null,
zip: addr.zip ?? null,
country: addr.country?.name ?? "United States",
};
zip: addr.zipCode ?? null,
country: addr.country ?? null,
phone: addr.phone ?? null,
}));
}
if (opts?.includePrimaryContact) {
+5 -2
View File
@@ -1476,14 +1476,17 @@ export class OpportunityController {
public async resequenceProducts(
orderedIds: number[]
): Promise<ForecastProductController[]> {
// Validate all IDs exist in the local ProductData table (the IDs the UI works with)
// Validate all IDs exist in the local ProductData table (the IDs the UI works with).
// Fall back to productSequence for items that were just added and haven't been
// synced to productData yet — appendProductSequenceIds writes them immediately.
const existingRows = await prisma.productData.findMany({
where: { opportunityId: this.cwOpportunityId },
select: { id: true },
});
const existingIds = new Set(existingRows.map((r) => r.id));
const sequenceIds = new Set(this.productSequence);
for (const id of orderedIds) {
if (!existingIds.has(id)) {
if (!existingIds.has(id) && !sequenceIds.has(id)) {
throw new GenericError({
status: 404,
name: "ForecastItemNotFound",
+59 -2
View File
@@ -46,7 +46,18 @@ export const opportunities = {
// Resolve optional local FKs — nullify any that don't exist locally yet
// (the sync may be behind; these are all nullable in the schema)
const [companyExists, contactExists, siteExists, typeExists] = await Promise.all([
const [
companyExists,
contactExists,
siteExists,
typeExists,
stageExists,
statusExists,
locationExists,
departmentExists,
primaryRepExists,
secondaryRepExists,
] = await Promise.all([
cwData.company?.id
? prisma.company.findFirst({ where: { id: cwData.company.id }, select: { id: true } })
: null,
@@ -59,21 +70,67 @@ export const opportunities = {
mapped.typeId != null
? prisma.opportunityType.findFirst({ where: { id: mapped.typeId }, select: { id: true } })
: null,
mapped.stageId != null
? prisma.opportunityStage.findFirst({ where: { id: mapped.stageId }, select: { id: true } })
: null,
mapped.statusId != null
? prisma.opportunityStatus.findFirst({ where: { id: mapped.statusId }, select: { id: true } })
: null,
mapped.locationId != null
? prisma.corporateLocation.findFirst({ where: { id: mapped.locationId }, select: { id: true } })
: null,
mapped.departmentId != null
? prisma.internalDepartment.findFirst({ where: { id: mapped.departmentId }, select: { id: true } })
: null,
mapped.primarySalesRepId != null
? prisma.user.findFirst({ where: { cwIdentifier: mapped.primarySalesRepId }, select: { cwIdentifier: true } })
: null,
mapped.secondarySalesRepId != null
? prisma.user.findFirst({ where: { cwIdentifier: mapped.secondarySalesRepId }, select: { cwIdentifier: true } })
: null,
]);
const companyId = companyExists?.id ?? null;
const contactId = contactExists?.id ?? null;
const siteId = siteExists?.id ?? null;
const typeId = typeExists?.id ?? null;
const stageId = stageExists?.id ?? null;
const statusId = statusExists?.id ?? null;
const locationId = locationExists?.id ?? null;
const departmentId = departmentExists?.id ?? null;
const primarySalesRepId = primaryRepExists?.cwIdentifier ?? null;
const secondarySalesRepId = secondaryRepExists?.cwIdentifier ?? null;
// Strip fields returned by mapCwToDb that are not columns in the Prisma schema
// (ratingName, ratingCwId, campaignName, primarySalesRepName, primarySalesRepIdentifier,
// secondarySalesRepName, secondarySalesRepIdentifier, cwLastUpdated).
// Prisma will throw a validation error if unknown fields are passed to create().
const {
ratingName: _ratingName,
ratingCwId: _ratingCwId,
campaignName: _campaignName,
primarySalesRepName: _primarySalesRepName,
primarySalesRepIdentifier: _primarySalesRepIdentifier,
secondarySalesRepName: _secondarySalesRepName,
secondarySalesRepIdentifier: _secondarySalesRepIdentifier,
cwLastUpdated: _cwLastUpdated,
...dbFields
} = mapped;
const record = await prisma.opportunity.create({
data: {
id: cwData.id,
...mapped,
...dbFields,
typeId,
stageId,
statusId,
locationId,
departmentId,
companyId,
contactId,
siteId,
primarySalesRepId,
secondarySalesRepId,
},
include: {
company: { include: { contacts: true, companyAddresses: true } },