fix: fix several different data parsing issues
This commit is contained in:
@@ -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 } },
|
||||
|
||||
Reference in New Issue
Block a user