Merge branch 'release/next'
This commit is contained in:
@@ -95,6 +95,7 @@ describe("FulfillmentService", () => {
|
||||
})
|
||||
|
||||
describe("createShipment", () => {
|
||||
const trackingLinkRepository = MockRepository({ create: c => c })
|
||||
const fulfillmentRepository = MockRepository({
|
||||
findOne: () => Promise.resolve({ id: IdMap.getId("fulfillment") }),
|
||||
})
|
||||
@@ -102,6 +103,7 @@ describe("FulfillmentService", () => {
|
||||
const fulfillmentService = new FulfillmentService({
|
||||
manager: MockManager,
|
||||
fulfillmentRepository,
|
||||
trackingLinkRepository,
|
||||
})
|
||||
|
||||
const now = new Date()
|
||||
@@ -113,14 +115,17 @@ describe("FulfillmentService", () => {
|
||||
it("calls order model functions", async () => {
|
||||
await fulfillmentService.createShipment(
|
||||
IdMap.getId("fulfillment"),
|
||||
["1234", "2345"],
|
||||
[{ tracking_number: "1234" }, { tracking_number: "2345" }],
|
||||
{}
|
||||
)
|
||||
|
||||
expect(fulfillmentRepository.save).toHaveBeenCalledTimes(1)
|
||||
expect(fulfillmentRepository.save).toHaveBeenCalledWith({
|
||||
id: IdMap.getId("fulfillment"),
|
||||
tracking_numbers: ["1234", "2345"],
|
||||
tracking_links: [
|
||||
{ tracking_number: "1234" },
|
||||
{ tracking_number: "2345" },
|
||||
],
|
||||
metadata: {},
|
||||
shipped_at: now,
|
||||
})
|
||||
|
||||
@@ -1182,14 +1182,14 @@ describe("OrderService", () => {
|
||||
await orderService.createShipment(
|
||||
IdMap.getId("test"),
|
||||
IdMap.getId("fulfillment"),
|
||||
["1234", "2345"],
|
||||
[{ tracking_number: "1234" }, { tracking_number: "2345" }],
|
||||
{}
|
||||
)
|
||||
|
||||
expect(fulfillmentService.createShipment).toHaveBeenCalledTimes(1)
|
||||
expect(fulfillmentService.createShipment).toHaveBeenCalledWith(
|
||||
IdMap.getId("fulfillment"),
|
||||
["1234", "2345"],
|
||||
[{ tracking_number: "1234" }, { tracking_number: "2345" }],
|
||||
{}
|
||||
)
|
||||
|
||||
|
||||
@@ -418,7 +418,7 @@ class ClaimService extends BaseService {
|
||||
})
|
||||
}
|
||||
|
||||
async createShipment(id, fulfillmentId, trackingNumbers, metadata = []) {
|
||||
async createShipment(id, fulfillmentId, trackingLinks, metadata = []) {
|
||||
return this.atomicPhase_(async manager => {
|
||||
const claim = await this.retrieve(id, {
|
||||
relations: ["additional_items"],
|
||||
@@ -426,7 +426,7 @@ class ClaimService extends BaseService {
|
||||
|
||||
const shipment = await this.fulfillmentService_
|
||||
.withTransaction(manager)
|
||||
.createShipment(fulfillmentId, trackingNumbers, metadata)
|
||||
.createShipment(fulfillmentId, trackingLinks, metadata)
|
||||
|
||||
claim.fulfillment_status = "shipped"
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ import Scrypt from "scrypt-kdf"
|
||||
import _ from "lodash"
|
||||
import { Validator, MedusaError } from "medusa-core-utils"
|
||||
import { BaseService } from "medusa-interfaces"
|
||||
import { Brackets } from "typeorm"
|
||||
|
||||
/**
|
||||
* Provides layer to manipulate customers.
|
||||
@@ -132,6 +133,50 @@ class CustomerService extends BaseService {
|
||||
return customerRepo.find(query)
|
||||
}
|
||||
|
||||
async listAndCount(
|
||||
selector,
|
||||
config = { relations: [], skip: 0, take: 50, order: { created_at: "DESC" } }
|
||||
) {
|
||||
const customerRepo = this.manager_.getCustomRepository(
|
||||
this.customerRepository_
|
||||
)
|
||||
|
||||
let q
|
||||
if ("q" in selector) {
|
||||
q = selector.q
|
||||
delete selector.q
|
||||
}
|
||||
|
||||
const query = this.buildQuery_(selector, config)
|
||||
|
||||
if (q) {
|
||||
const where = query.where
|
||||
|
||||
delete where.email
|
||||
delete where.first_name
|
||||
delete where.last_name
|
||||
|
||||
query.join = {
|
||||
alias: "customer",
|
||||
}
|
||||
|
||||
query.where = qb => {
|
||||
qb.where(where)
|
||||
|
||||
qb.andWhere(
|
||||
new Brackets(qb => {
|
||||
qb.where(`customer.first_name ILIKE :q`, { q: `%${q}%` })
|
||||
.orWhere(`customer.last_name ILIKE :q`, { q: `%${q}%` })
|
||||
.orWhere(`customer.email ILIKE :q`, { q: `%${q}%` })
|
||||
})
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
const [customers, count] = await customerRepo.findAndCount(query)
|
||||
return [customers, count]
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the total number of documents in database
|
||||
* @return {Promise} the result of the count operation
|
||||
|
||||
@@ -11,6 +11,7 @@ class FulfillmentService extends BaseService {
|
||||
manager,
|
||||
totalsService,
|
||||
fulfillmentRepository,
|
||||
trackingLinkRepository,
|
||||
shippingProfileService,
|
||||
lineItemService,
|
||||
fulfillmentProviderService,
|
||||
@@ -26,6 +27,9 @@ class FulfillmentService extends BaseService {
|
||||
/** @private @const {FulfillmentRepository} */
|
||||
this.fulfillmentRepository_ = fulfillmentRepository
|
||||
|
||||
/** @private @const {TrackingLinkRepository} */
|
||||
this.trackingLinkRepository_ = trackingLinkRepository
|
||||
|
||||
/** @private @const {ShippingProfileService} */
|
||||
this.shippingProfileService_ = shippingProfileService
|
||||
|
||||
@@ -44,6 +48,7 @@ class FulfillmentService extends BaseService {
|
||||
const cloned = new FulfillmentService({
|
||||
manager: transactionManager,
|
||||
totalsService: this.totalsService_,
|
||||
trackingLinkRepository: this.trackingLinkRepository_,
|
||||
fulfillmentRepository: this.fulfillmentRepository_,
|
||||
shippingProfileService: this.shippingProfileService_,
|
||||
lineItemService: this.lineItemService_,
|
||||
@@ -235,15 +240,18 @@ class FulfillmentService extends BaseService {
|
||||
* Creates a shipment by marking a fulfillment as shipped. Adds
|
||||
* tracking numbers and potentially more metadata.
|
||||
* @param {Order} fulfillmentId - the fulfillment to ship
|
||||
* @param {string[]} trackingNumbers - tracking numbers for the shipment
|
||||
* @param {TrackingLink[]} trackingNumbers - tracking numbers for the shipment
|
||||
* @param {object} metadata - potential metadata to add
|
||||
* @return {Fulfillment} the shipped fulfillment
|
||||
*/
|
||||
async createShipment(fulfillmentId, trackingNumbers, metadata) {
|
||||
async createShipment(fulfillmentId, trackingLinks, metadata) {
|
||||
return this.atomicPhase_(async manager => {
|
||||
const fulfillmentRepository = manager.getCustomRepository(
|
||||
this.fulfillmentRepository_
|
||||
)
|
||||
const trackingLinkRepo = manager.getCustomRepository(
|
||||
this.trackingLinkRepository_
|
||||
)
|
||||
|
||||
const fulfillment = await this.retrieve(fulfillmentId, {
|
||||
relations: ["items"],
|
||||
@@ -251,7 +259,11 @@ class FulfillmentService extends BaseService {
|
||||
|
||||
const now = new Date()
|
||||
fulfillment.shipped_at = now
|
||||
fulfillment.tracking_numbers = trackingNumbers
|
||||
|
||||
fulfillment.tracking_links = trackingLinks.map(tl =>
|
||||
trackingLinkRepo.create(tl)
|
||||
)
|
||||
|
||||
fulfillment.metadata = {
|
||||
...fulfillment.metadata,
|
||||
...metadata,
|
||||
|
||||
@@ -553,13 +553,13 @@ class OrderService extends BaseService {
|
||||
* have been created in regards to the shipment.
|
||||
* @param {string} orderId - the id of the order that has been shipped
|
||||
* @param {string} fulfillmentId - the fulfillment that has now been shipped
|
||||
* @param {Array<String>} trackingNumbers - array of tracking numebers
|
||||
* @param {TrackingLink[]} trackingLinks - array of tracking numebers
|
||||
* associated with the shipment
|
||||
* @param {Dictionary<String, String>} metadata - optional metadata to add to
|
||||
* the fulfillment
|
||||
* @return {order} the resulting order following the update.
|
||||
*/
|
||||
async createShipment(orderId, fulfillmentId, trackingNumbers, metadata = {}) {
|
||||
async createShipment(orderId, fulfillmentId, trackingLinks, metadata = {}) {
|
||||
return this.atomicPhase_(async manager => {
|
||||
const order = await this.retrieve(orderId, { relations: ["items"] })
|
||||
const shipment = await this.fulfillmentService_.retrieve(fulfillmentId)
|
||||
@@ -573,7 +573,7 @@ class OrderService extends BaseService {
|
||||
|
||||
const shipmentRes = await this.fulfillmentService_
|
||||
.withTransaction(manager)
|
||||
.createShipment(fulfillmentId, trackingNumbers, metadata)
|
||||
.createShipment(fulfillmentId, trackingLinks, metadata)
|
||||
|
||||
order.fulfillment_status = "shipped"
|
||||
for (const item of order.items) {
|
||||
|
||||
@@ -671,12 +671,12 @@ class SwapService extends BaseService {
|
||||
* @param {string} swapId - the id of the swap that has been shipped.
|
||||
* @param {string} fulfillmentId - the id of the specific fulfillment that
|
||||
* has been shipped
|
||||
* @param {Array<string>} trackingNumbers - the tracking numbers associated
|
||||
* @param {TrackingLink[]} trackingLinks - the tracking numbers associated
|
||||
* with the shipment
|
||||
* @param {object} metadata - optional metadata to attach to the shipment.
|
||||
* @returns {Promise<Swap>} the updated swap with new fulfillments and status.
|
||||
*/
|
||||
async createShipment(swapId, fulfillmentId, trackingNumbers, metadata = {}) {
|
||||
async createShipment(swapId, fulfillmentId, trackingLinks, metadata = {}) {
|
||||
return this.atomicPhase_(async manager => {
|
||||
const swap = await this.retrieve(swapId, {
|
||||
relations: ["additional_items"],
|
||||
@@ -685,7 +685,7 @@ class SwapService extends BaseService {
|
||||
// Update the fulfillment to register
|
||||
const shipment = await this.fulfillmentService_
|
||||
.withTransaction(manager)
|
||||
.createShipment(fulfillmentId, trackingNumbers, metadata)
|
||||
.createShipment(fulfillmentId, trackingLinks, metadata)
|
||||
|
||||
swap.fulfillment_status = "shipped"
|
||||
|
||||
|
||||
Reference in New Issue
Block a user