@@ -10,6 +10,7 @@ import {
1010 QBProductUpdateSchemaType ,
1111 QBProductUpdateSchema ,
1212 ProductChangedItemReferenceType ,
13+ ProductMappingSchemaType ,
1314} from '@/db/schema/qbProductSync'
1415import { ProductResponse , WhereClause } from '@/type/common'
1516import { ProductFlattenArrayResponseType } from '@/type/dto/api.dto'
@@ -32,6 +33,7 @@ import APIError from '@/app/api/core/exceptions/api'
3233import httpStatus from 'http-status'
3334import { QBSyncLog , QBSyncLogCreateSchemaType } from '@/db/schema/qbSyncLogs'
3435import User from '@/app/api/core/models/User.model'
36+ import { SettingService } from '@/app/api/quickbooks/setting/setting.service'
3537
3638export 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
0 commit comments