Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 28 additions & 6 deletions src/app/api/quickbooks/customer/customer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export class CustomerService extends BaseService {

async getByClientCompanyId(
clientCompanyId: string,
customerType: 'client' | 'company',
returningFields?: (keyof typeof QBCustomers)[],
) {
let columns = null
Expand All @@ -108,6 +109,7 @@ export class CustomerService extends BaseService {
and(
isNull(QBCustomers.deletedAt),
eq(QBCustomers.clientCompanyId, clientCompanyId),
eq(QBCustomers.customerType, customerType),
eq(QBCustomers.portalId, this.user.workspaceId),
),
...(columns && { columns }),
Expand Down Expand Up @@ -143,11 +145,11 @@ export class CustomerService extends BaseService {
}

const copilot = new CopilotAPI(this.user.token)
let client, company: CompanyResponse | undefined
let client

// get client and company info from copilot
if (clientId) client = await copilot.getClient(clientId)
if (companyId) company = await copilot.getCompany(companyId)
const company = await copilot.getCompany(companyId)

let clientCompany: ClientCompanyType = {
clientCompanyId:
Expand Down Expand Up @@ -231,6 +233,7 @@ export class CustomerService extends BaseService {
displayName,
email: client.email,
companyId: client.companyId,
companyName: company?.name || '',
},
companyInfo: company,
}
Expand Down Expand Up @@ -297,9 +300,13 @@ export class CustomerService extends BaseService {

async ensureCustomerExistsAndSyncToken(
clientCompanyId: string,
customerType: 'client' | 'company',
intuitApi: IntuitAPI,
) {
const existingCustomer = await this.getExistingCustomer(clientCompanyId)
const existingCustomer = await this.getExistingCustomer(
clientCompanyId,
customerType,
)

if (!existingCustomer) return

Expand All @@ -310,8 +317,11 @@ export class CustomerService extends BaseService {
)
}

async getExistingCustomer(clientCompanyId: string) {
return await this.getByClientCompanyId(clientCompanyId, [
async getExistingCustomer(
clientCompanyId: string,
customerType: 'client' | 'company',
) {
return await this.getByClientCompanyId(clientCompanyId, customerType, [
'id',
'qbCustomerId',
'qbSyncToken',
Expand All @@ -335,7 +345,7 @@ export class CustomerService extends BaseService {
invoiceResource: InvoiceCreatedResponseType['data']
}) {
const displayName = recipientInfo.displayName
// 2.1. search client in qb using recipient's email or
// 2.1. search client in qb using recipient's email or display name
let customer = recipientInfo.email
? await intuitApiService.getCustomerByEmail(recipientInfo.email)
: await intuitApiService.getACustomer(
Expand All @@ -344,6 +354,17 @@ export class CustomerService extends BaseService {
true,
)

// 2.2. verify the matched customer has the same company name. This is needed because a single customer with same email can be part of multiple companies
const sanitizedCompanyName = recipientInfo.companyName
? replaceSpecialCharsForQB(recipientInfo.companyName)
: undefined
if (
customer &&
(customer.CompanyName || undefined) !== sanitizedCompanyName
) {
customer = undefined
}

// 3. if not found, create a new client in the QB
if (!customer) {
console.info(
Expand Down Expand Up @@ -386,6 +407,7 @@ export class CustomerService extends BaseService {
displayName: recipientInfo.displayName,
email: recipientInfo.email,
companyName: companyInfo?.name,
customerType: recipientInfo.type,
qbSyncToken: customer.SyncToken,
qbCustomerId: customer.Id,
})
Expand Down
1 change: 1 addition & 0 deletions src/app/api/quickbooks/invoice/invoice.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,7 @@ export class InvoiceService extends BaseService {
const existingCustomer =
await customerService.ensureCustomerExistsAndSyncToken(
recipientInfo.clientCompanyId,
recipientInfo.type,
intuitApiService,
)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
DROP INDEX "uq_qb_customers_client_company_id_active_idx";--> statement-breakpoint
ALTER TABLE "qb_customers" ADD COLUMN "customer_type" varchar(20) DEFAULT 'client' NOT NULL;--> statement-breakpoint
CREATE UNIQUE INDEX "uq_qb_customers_client_company_id_type_active_idx" ON "qb_customers" USING btree ("portal_id","client_company_id","customer_type") WHERE "qb_customers"."deleted_at" is null;
Loading
Loading