fix: ensures no duplicate tax lines when completing cart (#1262)
* fix: ensures that no duplicate tax lines are created when completing cart
This commit is contained in:
1 parent
497e9b3f7d
commit
607a382b4e
9 files changed
+269
-16
No files matched your search
@@ -0,0 +1,23 @@
|
||||
import { MigrationInterface, QueryRunner } from "typeorm"
|
||||
|
||||
export class taxLineConstraints1648641130007 implements MigrationInterface {
|
||||
name = "taxLineConstraints1648641130007"
|
||||
|
||||
public async up(queryRunner: QueryRunner): Promise<void> {
|
||||
await queryRunner.query(
|
||||
`ALTER TABLE "line_item_tax_line" ADD CONSTRAINT "UQ_3c2af51043ed7243e7d9775a2ad" UNIQUE ("item_id", "code")`
|
||||
)
|
||||
await queryRunner.query(
|
||||
`ALTER TABLE "shipping_method_tax_line" ADD CONSTRAINT "UQ_cd147fca71e50bc954139fa3104" UNIQUE ("shipping_method_id", "code")`
|
||||
)
|
||||
}
|
||||
|
||||
public async down(queryRunner: QueryRunner): Promise<void> {
|
||||
await queryRunner.query(
|
||||
`ALTER TABLE "shipping_method_tax_line" DROP CONSTRAINT "UQ_cd147fca71e50bc954139fa3104"`
|
||||
)
|
||||
await queryRunner.query(
|
||||
`ALTER TABLE "line_item_tax_line" DROP CONSTRAINT "UQ_3c2af51043ed7243e7d9775a2ad"`
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,11 @@
|
||||
import {
|
||||
Entity,
|
||||
BeforeInsert,
|
||||
Index,
|
||||
Column,
|
||||
ManyToOne,
|
||||
Entity,
|
||||
Index,
|
||||
JoinColumn,
|
||||
ManyToOne,
|
||||
Unique,
|
||||
} from "typeorm"
|
||||
import { ulid } from "ulid"
|
||||
|
||||
@@ -12,6 +13,7 @@ import { TaxLine } from "./tax-line"
|
||||
import { LineItem } from "./line-item"
|
||||
|
||||
@Entity()
|
||||
@Unique(["item_id", "code"])
|
||||
export class LineItemTaxLine extends TaxLine {
|
||||
@Index()
|
||||
@Column()
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
import {
|
||||
Entity,
|
||||
BeforeInsert,
|
||||
Index,
|
||||
Column,
|
||||
ManyToOne,
|
||||
Entity,
|
||||
Index,
|
||||
JoinColumn,
|
||||
ManyToOne,
|
||||
Unique,
|
||||
} from "typeorm"
|
||||
import { ulid } from "ulid"
|
||||
|
||||
@@ -12,6 +13,7 @@ import { TaxLine } from "./tax-line"
|
||||
import { ShippingMethod } from "./shipping-method"
|
||||
|
||||
@Entity()
|
||||
@Unique(["shipping_method_id", "code"])
|
||||
export class ShippingMethodTaxLine extends TaxLine {
|
||||
@Index()
|
||||
@Column()
|
||||
|
||||
@@ -2,4 +2,36 @@ import { EntityRepository, Repository } from "typeorm"
|
||||
import { LineItemTaxLine } from "../models/line-item-tax-line"
|
||||
|
||||
@EntityRepository(LineItemTaxLine)
|
||||
export class LineItemTaxLineRepository extends Repository<LineItemTaxLine> {}
|
||||
export class LineItemTaxLineRepository extends Repository<LineItemTaxLine> {
|
||||
async upsertLines(lines: LineItemTaxLine[]): Promise<LineItemTaxLine[]> {
|
||||
const insertResult = await this.createQueryBuilder()
|
||||
.insert()
|
||||
.values(lines)
|
||||
.orUpdate({
|
||||
conflict_target: ["item_id", "code"],
|
||||
overwrite: ["rate", "name", "updated_at"],
|
||||
})
|
||||
.execute()
|
||||
|
||||
return insertResult.identifiers as LineItemTaxLine[]
|
||||
}
|
||||
|
||||
async deleteForCart(cartId: string): Promise<void> {
|
||||
const qb = this.createQueryBuilder("line")
|
||||
.select(["line.id"])
|
||||
.innerJoin("line_item", "i", "i.id = line.item_id")
|
||||
.innerJoin(
|
||||
"cart",
|
||||
"c",
|
||||
"i.cart_id = :cartId AND c.completed_at is NULL",
|
||||
{ cartId }
|
||||
)
|
||||
|
||||
const toDelete = await qb.getMany()
|
||||
|
||||
await this.createQueryBuilder()
|
||||
.delete()
|
||||
.whereInIds(toDelete.map((d) => d.id))
|
||||
.execute()
|
||||
}
|
||||
}
|
||||
@@ -2,4 +2,38 @@ import { EntityRepository, Repository } from "typeorm"
|
||||
import { ShippingMethodTaxLine } from "../models/shipping-method-tax-line"
|
||||
|
||||
@EntityRepository(ShippingMethodTaxLine)
|
||||
export class ShippingMethodTaxLineRepository extends Repository<ShippingMethodTaxLine> {}
|
||||
export class ShippingMethodTaxLineRepository extends Repository<ShippingMethodTaxLine> {
|
||||
async upsertLines(
|
||||
lines: ShippingMethodTaxLine[]
|
||||
): Promise<ShippingMethodTaxLine[]> {
|
||||
const insertResult = await this.createQueryBuilder()
|
||||
.insert()
|
||||
.values(lines)
|
||||
.orUpdate({
|
||||
conflict_target: ["shipping_method_id", "code"],
|
||||
overwrite: ["rate", "name", "updated_at"],
|
||||
})
|
||||
.execute()
|
||||
|
||||
return insertResult.identifiers as ShippingMethodTaxLine[]
|
||||
}
|
||||
|
||||
async deleteForCart(cartId: string): Promise<void> {
|
||||
const qb = this.createQueryBuilder("line")
|
||||
.select(["line.id"])
|
||||
.innerJoin("shipping_method", "sm", "sm.id = line.shipping_method_id")
|
||||
.innerJoin(
|
||||
"cart",
|
||||
"c",
|
||||
"sm.cart_id = :cartId AND c.completed_at is NULL",
|
||||
{ cartId }
|
||||
)
|
||||
|
||||
const toDelete = await qb.getMany()
|
||||
|
||||
await this.createQueryBuilder()
|
||||
.delete()
|
||||
.whereInIds(toDelete.map((d) => d.id))
|
||||
.execute()
|
||||
}
|
||||
}
|
||||
@@ -1213,6 +1213,13 @@ class CartService extends BaseService {
|
||||
return cartRepository.save(cart)
|
||||
}
|
||||
|
||||
if (!cart.payment_session) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_ALLOWED,
|
||||
"You cannot complete a cart without a payment session."
|
||||
)
|
||||
}
|
||||
|
||||
const session = await this.paymentProviderService_
|
||||
.withTransaction(manager)
|
||||
.authorizePayment(cart.payment_session, context)
|
||||
@@ -1874,9 +1881,8 @@ class CartService extends BaseService {
|
||||
})
|
||||
const calculationContext = this.totalsService_.getCalculationContext(cart)
|
||||
|
||||
await this.taxProviderService_
|
||||
.withTransaction(manager)
|
||||
.createTaxLines(cart, calculationContext)
|
||||
const txTaxProvider = this.taxProviderService_.withTransaction(manager)
|
||||
await txTaxProvider.createTaxLines(cart, calculationContext)
|
||||
|
||||
return cart
|
||||
})
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { MedusaError } from "medusa-core-utils"
|
||||
import { AwilixContainer } from "awilix"
|
||||
import { BaseService } from "medusa-interfaces"
|
||||
import { EntityManager } from "typeorm"
|
||||
import { EntityManager, UpdateResult } from "typeorm"
|
||||
import Redis from "ioredis"
|
||||
|
||||
import { LineItemTaxLineRepository } from "../repositories/line-item-tax-line"
|
||||
@@ -15,6 +15,7 @@ import { ShippingMethod } from "../models/shipping-method"
|
||||
import { Region } from "../models/region"
|
||||
import { Cart } from "../models/cart"
|
||||
import { isCart } from "../types/cart"
|
||||
import { PostgresError } from "../utils/exception-formatter"
|
||||
import {
|
||||
ITaxService,
|
||||
ItemTaxCalculationLine,
|
||||
@@ -94,6 +95,18 @@ class TaxProviderService extends BaseService {
|
||||
return provider
|
||||
}
|
||||
|
||||
async clearTaxLines(cartId: string): Promise<void> {
|
||||
const taxLineRepo = this.manager_.getCustomRepository(this.taxLineRepo_)
|
||||
const shippingTaxRepo = this.manager_.getCustomRepository(
|
||||
this.smTaxLineRepo_
|
||||
)
|
||||
|
||||
await Promise.all([
|
||||
taxLineRepo.deleteForCart(cartId),
|
||||
shippingTaxRepo.deleteForCart(cartId),
|
||||
])
|
||||
}
|
||||
|
||||
/**
|
||||
* Persists the tax lines relevant for an order to the database.
|
||||
* @param cartOrLineItems - the cart or line items to create tax lines for
|
||||
@@ -114,7 +127,33 @@ class TaxProviderService extends BaseService {
|
||||
taxLines = await this.getTaxLines(cartOrLineItems, calculationContext)
|
||||
}
|
||||
|
||||
return this.manager_.save(taxLines)
|
||||
const itemTaxLineRepo = this.manager_.getCustomRepository(this.taxLineRepo_)
|
||||
const shippingTaxLineRepo = this.manager_.getCustomRepository(
|
||||
this.smTaxLineRepo_
|
||||
)
|
||||
|
||||
const { shipping, lineItems } = taxLines.reduce<{
|
||||
shipping: ShippingMethodTaxLine[]
|
||||
lineItems: LineItemTaxLine[]
|
||||
}>(
|
||||
(acc, tl) => {
|
||||
if ("item_id" in tl) {
|
||||
acc.lineItems.push(tl)
|
||||
} else {
|
||||
acc.shipping.push(tl)
|
||||
}
|
||||
|
||||
return acc
|
||||
},
|
||||
{ shipping: [], lineItems: [] }
|
||||
)
|
||||
|
||||
return (
|
||||
await Promise.all([
|
||||
itemTaxLineRepo.upsertLines(lineItems),
|
||||
shippingTaxLineRepo.upsertLines(shipping),
|
||||
])
|
||||
).flat()
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in new issue
Block a user