diff --git a/packages/medusa/src/services/__tests__/custom-shipping-option.js b/packages/medusa/src/services/__tests__/custom-shipping-option.js index a4a8d31cfb..6536495501 100644 --- a/packages/medusa/src/services/__tests__/custom-shipping-option.js +++ b/packages/medusa/src/services/__tests__/custom-shipping-option.js @@ -4,7 +4,7 @@ import { MockManager, MockRepository, IdMap } from "medusa-test-utils" describe("CustomShippingOptionService", () => { describe("list", () => { const customShippingOptionRepository = MockRepository({ - find: q => { + find: (q) => { return Promise.resolve([ { id: "cso-test", @@ -44,7 +44,7 @@ describe("CustomShippingOptionService", () => { describe("retrieve", () => { const customShippingOptionRepository = MockRepository({ - findOne: q => { + findOne: (q) => { if (q.where.id === "cso-test") { return Promise.resolve({ id: "cso-test", @@ -88,8 +88,8 @@ describe("CustomShippingOptionService", () => { const customShippingOptionRepository = MockRepository({ create: jest .fn() - .mockImplementation(f => Promise.resolve({ id: "test-cso", ...f })), - save: jest.fn().mockImplementation(f => Promise.resolve(f)), + .mockImplementation((f) => Promise.resolve({ id: "test-cso", ...f })), + save: jest.fn().mockImplementation((f) => Promise.resolve(f)), }) const customShippingOptionService = new CustomShippingOptionService({ @@ -114,7 +114,7 @@ describe("CustomShippingOptionService", () => { cart_id: "test-cso-cart", shipping_option_id: "test-so", price: 30, - metadata: {}, + metadata: undefined, }) expect(customShippingOptionRepository.save).toHaveBeenCalledTimes(1) @@ -123,7 +123,7 @@ describe("CustomShippingOptionService", () => { cart_id: "test-cso-cart", shipping_option_id: "test-so", price: 30, - metadata: {}, + metadata: undefined, }) }) }) diff --git a/packages/medusa/src/services/custom-shipping-option.js b/packages/medusa/src/services/custom-shipping-option.js deleted file mode 100644 index e45c86331e..0000000000 --- a/packages/medusa/src/services/custom-shipping-option.js +++ /dev/null @@ -1,111 +0,0 @@ -import { MedusaError } from "medusa-core-utils" -import { BaseService } from "medusa-interfaces" - -class CustomShippingOptionService extends BaseService { - constructor({ manager, customShippingOptionRepository }) { - super() - - /** @private @const {EntityManager} */ - this.manager_ = manager - - /** @private @const {CustomShippingOptionRepository} */ - this.customShippingOptionRepository_ = customShippingOptionRepository - } - - /** - * Sets the service's manager to a given transaction manager - * @param {EntityManager} manager - the manager to use - * @return {CustomShippingOptionService} a cloned CustomShippingOption service - */ - withTransaction(manager) { - if (!manager) { - return this - } - - const cloned = new CustomShippingOptionService({ - manager, - customShippingOptionRepository: this.customShippingOptionRepository_, - }) - - cloned.transactionManager_ = manager - return cloned - } - - /** - * Retrieves a specific shipping option. - * @param {string} id - the id of the custom shipping option to retrieve. - * @param {*} config - any options needed to query for the result. - * @return {Promise} which resolves to the requested custom shipping option. - */ - async retrieve(id, config = {}) { - const customShippingOptionRepo = this.manager_.getCustomRepository( - this.customShippingOptionRepository_ - ) - - const validatedId = this.validateId_(id) - const query = this.buildQuery_({ id: validatedId }, config) - - const customShippingOption = await customShippingOptionRepo.findOne(query) - - if (!customShippingOption) { - throw new MedusaError( - MedusaError.Types.NOT_FOUND, - `Custom shipping option with id: ${id} was not found.` - ) - } - - return customShippingOption - } - - /** Fetches all custom shipping options related to the given selector - * @param {Object} selector - the query object for find - * @param {Object} config - the configuration used to find the objects. contains relations, skip, and take. - * @return {Promise} custom shipping options matching the query - */ - async list( - selector, - config = { - skip: 0, - take: 50, - relations: [], - } - ) { - const customShippingOptionRepo = this.manager_.getCustomRepository( - this.customShippingOptionRepository_ - ) - - const query = this.buildQuery_(selector, config) - - return customShippingOptionRepo.find(query) - } - - /** - * Creates a custom shipping option associated with a given author - * @param {object} data - the custom shipping option to create - * @param {*} config - any configurations if needed, including meta data - * @return {Promise} resolves to the creation result - */ - async create(data, config = { metadata: {} }) { - const { metadata } = config - - const { cart_id, shipping_option_id, price } = data - - return this.atomicPhase_(async (manager) => { - const customShippingOptionRepo = manager.getCustomRepository( - this.customShippingOptionRepository_ - ) - - const customShippingOption = await customShippingOptionRepo.create({ - cart_id, - shipping_option_id, - price, - metadata, - }) - const result = await customShippingOptionRepo.save(customShippingOption) - - return result - }) - } -} - -export default CustomShippingOptionService diff --git a/packages/medusa/src/services/custom-shipping-option.ts b/packages/medusa/src/services/custom-shipping-option.ts new file mode 100644 index 0000000000..3c437b62e3 --- /dev/null +++ b/packages/medusa/src/services/custom-shipping-option.ts @@ -0,0 +1,111 @@ +import { MedusaError } from "medusa-core-utils" +import { EntityManager } from "typeorm" +import { TransactionBaseService } from "../interfaces" +import { CustomShippingOption } from "../models" +import { CustomShippingOptionRepository } from "../repositories/custom-shipping-option" +import { FindConfig, Selector } from "../types/common" +import { CreateCustomShippingOptionInput } from "../types/shipping-options" +import { buildQuery } from "../utils" + +type InjectedDependencies = { + manager: EntityManager + customShippingOptionRepository: typeof CustomShippingOptionRepository +} +class CustomShippingOptionService extends TransactionBaseService { + protected manager_: EntityManager + protected transactionManager_: EntityManager | undefined + protected customShippingOptionRepository_: typeof CustomShippingOptionRepository + + constructor({ + manager, + customShippingOptionRepository, + }: InjectedDependencies) { + // eslint-disable-next-line prefer-rest-params + super(arguments[0]) + + this.manager_ = manager + this.customShippingOptionRepository_ = customShippingOptionRepository + } + + /** + * Retrieves a specific shipping option. + * @param id - the id of the custom shipping option to retrieve. + * @param config - any options needed to query for the result. + * @return the requested custom shipping option. + */ + async retrieve( + id: string, + config: FindConfig = {} + ): Promise { + return await this.atomicPhase_(async (manager) => { + const customShippingOptionRepo = manager.getCustomRepository( + this.customShippingOptionRepository_ + ) + + const query = buildQuery({ id }, config) + + const customShippingOption = await customShippingOptionRepo.findOne(query) + + if (!customShippingOption) { + throw new MedusaError( + MedusaError.Types.NOT_FOUND, + `Custom shipping option with id: ${id} was not found.` + ) + } + + return customShippingOption + }) + } + + /** Fetches all custom shipping options based on the given selector + * @param selector - the query object for find + * @param config - the configuration used to find the objects. contains relations, skip, and take. + * @return custom shipping options matching the query + */ + async list( + selector: Selector, + config: FindConfig = { + skip: 0, + take: 50, + relations: [], + } + ): Promise { + return await this.atomicPhase_(async (manager) => { + const customShippingOptionRepo = manager.getCustomRepository( + this.customShippingOptionRepository_ + ) + + const query = buildQuery(selector, config) + + return await customShippingOptionRepo.find(query) + }) + } + + /** + * Creates a custom shipping option + * @param data - the custom shipping option to create + * @param config - any configurations if needed, including meta data + * @return resolves to the creation result + */ + async create( + data: CreateCustomShippingOptionInput + ): Promise { + const { cart_id, shipping_option_id, price, metadata } = data + + return await this.atomicPhase_(async (manager) => { + const customShippingOptionRepo = manager.getCustomRepository( + this.customShippingOptionRepository_ + ) + + const customShippingOption = await customShippingOptionRepo.create({ + cart_id, + shipping_option_id, + price, + metadata, + }) + return await customShippingOptionRepo.save(customShippingOption) + }) + } +} + +export default CustomShippingOptionService diff --git a/packages/medusa/src/types/shipping-options.ts b/packages/medusa/src/types/shipping-options.ts index ebf75019f9..4b03a84168 100644 --- a/packages/medusa/src/types/shipping-options.ts +++ b/packages/medusa/src/types/shipping-options.ts @@ -52,6 +52,13 @@ export type CreateShippingOptionInput = { requirements?: ShippingOptionRequirement[] } +export type CreateCustomShippingOptionInput = { + price: number + shipping_option_id: string + cart_id?: string + metadata?: Record +} + export type UpdateShippingOptionInput = { metadata?: Record price_type?: ShippingOptionPriceType