feat: allow custom address on claims

This commit is contained in:
Sebastian Rindom
2021-05-27 12:29:11 +02:00
parent fd26f3716d
commit 586f4d884c
5 changed files with 138 additions and 28 deletions
@@ -85,6 +85,9 @@ import { defaultRelations, defaultFields } from "./"
* price:
* description: The price to charge for the Shipping Method
* type: integer
* shipping_address:
* type: object
* description: "An optional shipping address to send the claim to. Defaults to the parent order's shipping address"
* refund_amount:
* description: The amount to refund the Customer when the Claim type is `refund`.
* type: integer
@@ -151,6 +154,7 @@ export default async (req, res) => {
refund_amount: Validator.number()
.integer()
.optional(),
shipping_address: Validator.object().optional(),
metadata: Validator.object().optional(),
})
@@ -203,6 +207,7 @@ export default async (req, res) => {
idempotency_key: idempotencyKey.idempotency_key,
order,
type: value.type,
shipping_address: value.shipping_address,
claim_items: value.claim_items,
return_shipping: value.return_shipping,
additional_items: value.additional_items,
+21 -1
View File
@@ -16,6 +16,7 @@ class ClaimService extends BaseService {
constructor({
manager,
claimRepository,
addressRepository,
fulfillmentProviderService,
fulfillmentService,
lineItemService,
@@ -35,6 +36,9 @@ class ClaimService extends BaseService {
/** @private @constant {ClaimRepository} */
this.claimRepository_ = claimRepository
/** @private @constant {AddressRepository} */
this.addressRepo_ = addressRepository
/** @private @constant {FulfillmentProviderService} */
this.fulfillmentProviderService_ = fulfillmentProviderService
@@ -74,6 +78,7 @@ class ClaimService extends BaseService {
const cloned = new ClaimService({
manager,
claimRepository: this.claimRepository_,
addressRepository: this.addressRepo_,
fulfillmentProviderService: this.fulfillmentProviderService_,
fulfillmentService: this.fulfillmentService_,
paymentProviderService: this.paymentProviderService_,
@@ -150,6 +155,11 @@ class ClaimService extends BaseService {
})
}
/**
* Creates a Claim on an Order. Claims consists of items that are claimed and
* optionally items to be sent as replacement for the claimed items. The
* shipping address that the new items will be shipped to
*/
create(data) {
return this.atomicPhase_(async manager => {
const claimRepo = manager.getCustomRepository(this.claimRepository_)
@@ -162,9 +172,19 @@ class ClaimService extends BaseService {
additional_items,
shipping_methods,
refund_amount,
shipping_address,
shipping_address_id,
...rest
} = data
let addressId = shipping_address_id || order.shipping_address_id
if (shipping_address) {
const addressRepo = manager.getCustomRepository(this.addressRepo_)
const created = addressRepo.create(shipping_address)
const saved = await addressRepo.save(created)
addressId = saved.id
}
if (type !== "refund" && type !== "replace") {
throw new MedusaError(
MedusaError.Types.INVALID_DATA,
@@ -214,7 +234,7 @@ class ClaimService extends BaseService {
)
const created = claimRepo.create({
shipping_address_id: order.shipping_address_id,
shipping_address_id: addressId,
payment_status: type === "refund" ? "not_refunded" : "na",
...rest,
refund_amount: toRefund,