Feat (medusa): convert custom shipping option service to typescript (#1652)
* convert custom shipping option service to typescript * polishing * remove unnecessary imports * Update packages/medusa/src/services/custom-shipping-option.ts Co-authored-by: Zakaria El Asri <33696020+zakariaelas@users.noreply.github.com> * pr feedback * fix typing after removing optional metadataparameter * add injected dependencies * add await Co-authored-by: Zakaria El Asri <33696020+zakariaelas@users.noreply.github.com>
This commit is contained in:
@@ -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,
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -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<CustomShippingOption>} 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<CustomShippingOption[]>} 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<CustomShippingOption>} 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
|
||||
111
packages/medusa/src/services/custom-shipping-option.ts
Normal file
111
packages/medusa/src/services/custom-shipping-option.ts
Normal file
@@ -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<CustomShippingOptionService> {
|
||||
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<CustomShippingOption> = {}
|
||||
): Promise<CustomShippingOption> {
|
||||
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<CustomShippingOption>,
|
||||
config: FindConfig<CustomShippingOption> = {
|
||||
skip: 0,
|
||||
take: 50,
|
||||
relations: [],
|
||||
}
|
||||
): Promise<CustomShippingOption[]> {
|
||||
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<CustomShippingOption> {
|
||||
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
|
||||
@@ -52,6 +52,13 @@ export type CreateShippingOptionInput = {
|
||||
requirements?: ShippingOptionRequirement[]
|
||||
}
|
||||
|
||||
export type CreateCustomShippingOptionInput = {
|
||||
price: number
|
||||
shipping_option_id: string
|
||||
cart_id?: string
|
||||
metadata?: Record<string, unknown>
|
||||
}
|
||||
|
||||
export type UpdateShippingOptionInput = {
|
||||
metadata?: Record<string, unknown>
|
||||
price_type?: ShippingOptionPriceType
|
||||
|
||||
Reference in New Issue
Block a user