Skip to content

Commit 92102ff

Browse files
fix(OUT-2111): multiple prices mapping, unmaps the existing price and product
- [X] replaced delete all mechanisms with update one, on the basis of product id and price id
1 parent 01631b6 commit 92102ff

3 files changed

Lines changed: 64 additions & 23 deletions

File tree

src/app/api/quickbooks/product/product.controller.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,8 @@ export async function storeProductMap(req: NextRequest) {
2222
const productService = new ProductService(user)
2323
const body = await req.json()
2424
const parsedBody = ProductMappingSchema.parse(body)
25-
const products = await productService.bulkDeleteCreateQBProduct(
26-
parsedBody.mappingItems,
27-
)
25+
const products = await productService.handleProductMap(parsedBody)
26+
2827
if (parsedBody.changedItemReference.length > 0)
2928
await productService.formatAndSyncProductLogs(
3029
parsedBody.changedItemReference,

src/app/api/quickbooks/product/product.service.ts

Lines changed: 60 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
QBProductUpdateSchemaType,
1111
QBProductUpdateSchema,
1212
ProductChangedItemReferenceType,
13+
ProductMappingSchemaType,
1314
} from '@/db/schema/qbProductSync'
1415
import { ProductResponse, WhereClause } from '@/type/common'
1516
import { ProductFlattenArrayResponseType } from '@/type/dto/api.dto'
@@ -32,6 +33,7 @@ import APIError from '@/app/api/core/exceptions/api'
3233
import httpStatus from 'http-status'
3334
import { QBSyncLog, QBSyncLogCreateSchemaType } from '@/db/schema/qbSyncLogs'
3435
import User from '@/app/api/core/models/User.model'
36+
import { SettingService } from '@/app/api/quickbooks/setting/setting.service'
3537

3638
export class ProductService extends BaseService {
3739
private syncLogService: SyncLogService
@@ -170,30 +172,68 @@ export class ProductService extends BaseService {
170172
}
171173

172174
/**
173-
* Bulk update or create the map between product, price with QB item
175+
* On intial save, save all flatten products. If mapped, we include and if not, those are excluded
176+
* On every save after that, we update the record on the basis of productId and priceId
174177
*/
175-
async bulkDeleteCreateQBProduct(
176-
payload: QBProductCreateArraySchemaType,
178+
async handleProductMap(
179+
body: ProductMappingSchemaType,
177180
returningFields?: (keyof typeof QBProductSync)[],
178-
): Promise<Partial<QBProductSelectSchemaType>[] | undefined> {
179-
const formattedPayload = payload.map((item) => {
180-
return {
181-
...item,
182-
portalId: this.user.workspaceId,
183-
}
184-
})
181+
) {
182+
const { mappingItems, changedItemReference } = body
183+
const settingService = new SettingService(this.user)
184+
const setting = await settingService.getOneByPortalId([
185+
'initialProductSettingMap',
186+
])
185187

186188
return await this.db.transaction(async (tx) => {
187-
await tx
188-
.delete(QBProductSync)
189-
.where(eq(QBProductSync.portalId, this.user.workspaceId))
190-
const query = tx.insert(QBProductSync).values(formattedPayload)
191-
const product = returningFields?.length
192-
? await query.returning(
193-
buildReturningFields(QBProductSync, returningFields),
194-
)
195-
: await query.returning()
196-
return product
189+
this.setTransaction(tx)
190+
191+
if (!setting?.initialProductSettingMap) {
192+
const formattedPayload = mappingItems.map((item) => {
193+
return {
194+
...item,
195+
copilotName: item.name,
196+
portalId: this.user.workspaceId,
197+
}
198+
})
199+
const query = this.db.insert(QBProductSync).values(formattedPayload)
200+
const products = returningFields?.length
201+
? await query.returning(
202+
buildReturningFields(QBProductSync, returningFields),
203+
)
204+
: await query.returning()
205+
return products
206+
}
207+
208+
if (changedItemReference.length > 0) {
209+
Promise.all(
210+
changedItemReference?.map(async (item) => {
211+
const payload = {
212+
portalId: this.user.workspaceId,
213+
productId: item.id,
214+
priceId: item.priceId,
215+
name: item.isExcluded ? null : item.qbItem?.name,
216+
description: item.isExcluded ? null : item.qbItem?.description,
217+
qbItemId: item.isExcluded ? null : item.qbItem?.id,
218+
qbSyncToken: item.isExcluded ? null : item.qbItem?.syncToken,
219+
copilotName: item.name,
220+
unitPrice: item.isExcluded
221+
? null
222+
: item.qbItem?.numericPrice.toString(),
223+
isExcluded: item.isExcluded,
224+
}
225+
const conditions = and(
226+
eq(QBProductSync.portalId, this.user.workspaceId),
227+
eq(QBProductSync.productId, item.id),
228+
eq(QBProductSync.priceId, item.priceId),
229+
) as WhereClause
230+
await this.updateOrCreateQBProduct(payload, conditions)
231+
}),
232+
)
233+
}
234+
235+
this.unsetTransaction()
236+
return await this.getAll()
197237
})
198238
}
199239

src/db/schema/qbProductSync.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ const QBItemSchema = z.object({
8080
name: z.string(),
8181
syncToken: z.string(),
8282
numericPrice: z.number(),
83+
description: z.string(),
8384
})
8485

8586
const ProductChangedItemReferenceSchema = z.object({
@@ -100,3 +101,4 @@ export const ProductMappingSchema = z.object({
100101
mappingItems: QBProductCreateArraySchema,
101102
changedItemReference: z.array(ProductChangedItemReferenceSchema),
102103
})
104+
export type ProductMappingSchemaType = z.infer<typeof ProductMappingSchema>

0 commit comments

Comments
 (0)