Skip to content

Commit

Permalink
@medusa/types
Browse files Browse the repository at this point in the history
  • Loading branch information
carlos-r-l-rodrigues committed Mar 13, 2023
1 parent fd4c1db commit 6bb69e2
Show file tree
Hide file tree
Showing 9 changed files with 161 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export default async (req, res) => {
.withTransaction(transactionManager)
.removeLocation(id)

await stockLocationService.withTransaction(transactionManager).delete(id)
await stockLocationService.delete(id)
})

res.status(200).send({
Expand Down
7 changes: 2 additions & 5 deletions packages/medusa/src/interfaces/services/stock-location.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import { EntityManager } from "typeorm"
import { FindConfig } from "../../types/common"

import {
StockLocationDTO,
FilterableStockLocationProps,
CreateStockLocationInput,
FilterableStockLocationProps,
StockLocationDTO,
UpdateStockLocationInput,
} from "../../types/stock-location"

export interface IStockLocationService {
withTransaction(transactionManager?: EntityManager): this
list(
selector: FilterableStockLocationProps,
config?: FindConfig<StockLocationDTO>
Expand Down
4 changes: 3 additions & 1 deletion packages/stock-location/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"license": "MIT",
"devDependencies": {
"@medusajs/medusa": "^1.7.7",
"@medusajs/types": "*",
"cross-env": "^5.2.1",
"jest": "^25.5.4",
"ts-jest": "^25.5.1",
Expand All @@ -36,6 +37,7 @@
"test:unit": "jest --passWithNoTests"
},
"peerDependencies": {
"@medusajs/medusa": "^1.7.7"
"@medusajs/medusa": "^1.7.7",
"@medusajs/types": "^0.0.1"
}
}
123 changes: 80 additions & 43 deletions packages/stock-location/src/services/stock-location.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,12 @@ import {
IEventBusService,
setMetadata,
StockLocationAddressInput,
TransactionBaseService,
UpdateStockLocationInput,
} from "@medusajs/medusa"

import { InternalModuleDeclaration } from "@medusajs/modules-sdk"

import { SharedContext } from "@medusajs/types"
import { isDefined, MedusaError } from "medusa-core-utils"
import { EntityManager } from "typeorm"

import { StockLocation, StockLocationAddress } from "../models"

type InjectedDependencies = {
Expand All @@ -26,23 +23,25 @@ type InjectedDependencies = {
* Service for managing stock locations.
*/

export default class StockLocationService extends TransactionBaseService {
export default class StockLocationService {
static Events = {
CREATED: "stock-location.created",
UPDATED: "stock-location.updated",
DELETED: "stock-location.deleted",
}

protected readonly manager_: EntityManager
protected readonly eventBusService_: IEventBusService

constructor(
{ eventBusService }: InjectedDependencies,
{ eventBusService, manager }: InjectedDependencies,
options?: unknown,
moduleDeclaration?: InternalModuleDeclaration
) {
// @ts-ignore
super(...arguments)

this.manager_ = manager
this.eventBusService_ = eventBusService
}

Expand All @@ -54,9 +53,10 @@ export default class StockLocationService extends TransactionBaseService {
*/
async list(
selector: FilterableStockLocationProps = {},
config: FindConfig<StockLocation> = { relations: [], skip: 0, take: 10 }
config: FindConfig<StockLocation> = { relations: [], skip: 0, take: 10 },
context: SharedContext = {}
): Promise<StockLocation[]> {
const manager = this.activeManager_
const manager = context.transactionManager ?? this.manager_
const locationRepo = manager.getRepository(StockLocation)

const query = buildQuery(selector, config)
Expand All @@ -71,9 +71,10 @@ export default class StockLocationService extends TransactionBaseService {
*/
async listAndCount(
selector: FilterableStockLocationProps = {},
config: FindConfig<StockLocation> = { relations: [], skip: 0, take: 10 }
config: FindConfig<StockLocation> = { relations: [], skip: 0, take: 10 },
context: SharedContext = {}
): Promise<[StockLocation[], number]> {
const manager = this.activeManager_
const manager = context.transactionManager ?? this.manager_
const locationRepo = manager.getRepository(StockLocation)

const query = buildQuery(selector, config)
Expand All @@ -89,7 +90,8 @@ export default class StockLocationService extends TransactionBaseService {
*/
async retrieve(
stockLocationId: string,
config: FindConfig<StockLocation> = {}
config: FindConfig<StockLocation> = {},
context: SharedContext = {}
): Promise<StockLocation> {
if (!isDefined(stockLocationId)) {
throw new MedusaError(
Expand All @@ -98,7 +100,7 @@ export default class StockLocationService extends TransactionBaseService {
)
}

const manager = this.activeManager_
const manager = context.transactionManager ?? this.manager_
const locationRepo = manager.getRepository(StockLocation)

const query = buildQuery({ id: stockLocationId }, config)
Expand All @@ -119,9 +121,13 @@ export default class StockLocationService extends TransactionBaseService {
* @param data - The input data for creating a Stock Location.
* @returns The created stock location.
*/
async create(data: CreateStockLocationInput): Promise<StockLocation> {
return await this.atomicPhase_(async (manager) => {
const locationRepo = manager.getRepository(StockLocation)
async create(
data: CreateStockLocationInput,
context: SharedContext = {}
): Promise<StockLocation> {
const execute = async (context: SharedContext) => {
const { transactionManager: manager } = context
const locationRepo = manager!.getRepository(StockLocation)

const loc = locationRepo.create({
name: data.name,
Expand All @@ -132,7 +138,7 @@ export default class StockLocationService extends TransactionBaseService {
const addrId = (data.address ?? data.address_id) as string
loc.address_id = addrId
} else {
const locAddressRepo = manager.getRepository(StockLocationAddress)
const locAddressRepo = manager!.getRepository(StockLocationAddress)
const locAddress = locAddressRepo.create(data.address!)
const addressResult = await locAddressRepo.save(locAddress)
loc.address_id = addressResult.id
Expand All @@ -146,13 +152,19 @@ export default class StockLocationService extends TransactionBaseService {

const result = await locationRepo.save(loc)

await this.eventBusService_
.withTransaction(manager)
.emit(StockLocationService.Events.CREATED, {
id: result.id,
})
await this.eventBusService_.emit(StockLocationService.Events.CREATED, {
id: result.id,
})

return result
}

if (context.transactionManager) {
return await execute(context)
}

return await this.manager_.transaction(async (transactionManager) => {
return await execute({ transactionManager })
})
}

Expand All @@ -165,20 +177,22 @@ export default class StockLocationService extends TransactionBaseService {

async update(
stockLocationId: string,
updateData: UpdateStockLocationInput
updateData: UpdateStockLocationInput,
context: SharedContext = {}
): Promise<StockLocation> {
return await this.atomicPhase_(async (manager) => {
const locationRepo = manager.getRepository(StockLocation)
const execute = async (context: SharedContext) => {
const { transactionManager: manager } = context
const locationRepo = manager!.getRepository(StockLocation)

const item = await this.retrieve(stockLocationId)
const item = await this.retrieve(stockLocationId, undefined, context)

const { address, ...data } = updateData

if (address) {
if (item.address_id) {
await this.updateAddress(item.address_id, address)
await this.updateAddress(item.address_id, address, context)
} else {
const locAddressRepo = manager.getRepository(StockLocationAddress)
const locAddressRepo = manager!.getRepository(StockLocationAddress)
const locAddress = locAddressRepo.create(address)
const addressResult = await locAddressRepo.save(locAddress)
data.address_id = addressResult.id
Expand All @@ -194,13 +208,19 @@ export default class StockLocationService extends TransactionBaseService {

await locationRepo.save(toSave)

await this.eventBusService_
.withTransaction(manager)
.emit(StockLocationService.Events.UPDATED, {
id: stockLocationId,
})
await this.eventBusService_.emit(StockLocationService.Events.UPDATED, {
id: stockLocationId,
})

return item
}

if (context.transactionManager) {
return await execute(context)
}

return await this.manager_.transaction(async (transactionManager) => {
return await execute({ transactionManager })
})
}

Expand All @@ -213,7 +233,8 @@ export default class StockLocationService extends TransactionBaseService {

protected async updateAddress(
addressId: string,
address: StockLocationAddressInput
address: StockLocationAddressInput,
context: SharedContext = {}
): Promise<StockLocationAddress> {
if (!isDefined(addressId)) {
throw new MedusaError(
Expand All @@ -222,8 +243,9 @@ export default class StockLocationService extends TransactionBaseService {
)
}

return await this.atomicPhase_(async (manager) => {
const locationAddressRepo = manager.getRepository(StockLocationAddress)
const execute = async (context: SharedContext) => {
const { transactionManager: manager } = context
const locationAddressRepo = manager!.getRepository(StockLocationAddress)

const existingAddress = await locationAddressRepo.findOne({
where: { id: addressId },
Expand All @@ -243,6 +265,14 @@ export default class StockLocationService extends TransactionBaseService {
}

return await locationAddressRepo.save(toSave)
}

if (context.transactionManager) {
return await execute(context)
}

return await this.manager_.transaction(async (transactionManager) => {
return await execute({ transactionManager })
})
}

Expand All @@ -251,17 +281,24 @@ export default class StockLocationService extends TransactionBaseService {
* @param id - The ID of the stock location to delete.
* @returns An empty promise.
*/
async delete(id: string): Promise<void> {
return await this.atomicPhase_(async (manager) => {
const locationRepo = manager.getRepository(StockLocation)
async delete(id: string, context: SharedContext = {}): Promise<void> {
const execute = async (context: SharedContext) => {
const { transactionManager: manager } = context
const locationRepo = manager!.getRepository(StockLocation)

await locationRepo.softRemove({ id })

await this.eventBusService_
.withTransaction(manager)
.emit(StockLocationService.Events.DELETED, {
id,
})
await this.eventBusService_.emit(StockLocationService.Events.DELETED, {
id,
})
}

if (context.transactionManager) {
return await execute(context)
}

return await this.manager_.transaction(async (transactionManager) => {
return await execute({ transactionManager })
})
}
}
30 changes: 30 additions & 0 deletions packages/types/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "@medusajs/types",
"version": "0.0.1",
"description": "Medusa Types definition",
"main": "dist/index.js",
"repository": {
"type": "git",
"url": "https:/medusajs/medusa",
"directory": "packages/types"
},
"publishConfig": {
"access": "public"
},
"files": [
"dist"
],
"author": "Medusa",
"license": "MIT",
"devDependencies": {
"cross-env": "^5.2.1",
"typeorm": "^0.3.11",
"typescript": "^4.4.4"
},
"scripts": {
"prepare": "cross-env NODE_ENV=production yarn run build",
"build": "tsc --build",
"test": "echo \"Tests disabled temporarily\"",
"test:unit": "echo \"Tests disabled temporarily\""
}
}
1 change: 1 addition & 0 deletions packages/types/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./shared-context"
5 changes: 5 additions & 0 deletions packages/types/src/shared-context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { EntityManager } from "typeorm"

export type SharedContext = {
transactionManager?: EntityManager
}
27 changes: 27 additions & 0 deletions packages/types/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"compilerOptions": {
"lib": ["es2020"],
"target": "es2020",
"outDir": "./dist",
"esModuleInterop": true,
"declaration": true,
"module": "commonjs",
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"sourceMap": true,
"noImplicitReturns": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"noImplicitThis": true,
"allowJs": true,
"skipLibCheck": true
},
"include": ["./src/**/*"],

This comment has been minimized.

Copy link
@adrien2p

adrien2p Mar 13, 2023

Member

suggestion: "src"

"exclude": [
"./dist/**/*",

This comment has been minimized.

Copy link
@adrien2p

adrien2p Mar 13, 2023

Member

suggestion: "dist"

"./src/**/__tests__",
"./src/**/__mocks__",
"node_modules"
]
}
Loading

0 comments on commit 6bb69e2

Please sign in to comment.