fix: working api

This commit is contained in:
Sebastian Rindom committed 2021-04-06 15:01:13 +02:00
1 parent 2b2555004e
commit 9d810971a7
8 files changed
+86 -49

No files matched your search

@@ -41,6 +41,7 @@
"dependencies": {
"@medusajs/medusa": "^1.1.17",
"body-parser": "^1.19.0",
"express": "^4.17.1",
"medusa-core-utils": "^1.1.3"
},
"gitHead": "0646bd395a6056657cb0aa93c13699c4a9dbbcdd"
@@ -1,7 +1,7 @@
import { Validator, MedusaError } from "medusa-core-utils"
export default async (req, res) => {
const { variant_id } = req.parmas
const { variant_id } = req.params
const schema = Validator.object().keys({
email: Validator.string().required(),
@@ -9,7 +9,8 @@ export default async (req, res) => {
const { value, error } = schema.validate(req.body)
if (error) {
throw new MedusaError(MedusaError.Types.INVALID_DATA, error.details)
res.status(400).json({ message: error.message })
return
}
try {
@@ -19,6 +20,6 @@ export default async (req, res) => {
await restockNotificationService.addEmail(variant_id, value.email)
res.sendStatus(200)
} catch (err) {
res.sendStatus(400).json({ message: err.message })
res.status(400).json({ message: err.message })
}
}
@@ -9,7 +9,7 @@ export default (app) => {
route.post(
"/variants/:variant_id",
bodyParser.raw({ type: "application/json" }),
bodyParser.json(),
middlewares.wrap(require("./add-email").default)
)
return app
@@ -16,6 +16,23 @@ class RestockNotificationService extends BaseService {
this.eventBus_ = eventBusService
}
withTransaction(transactionManager) {
if (!transactionManager) {
return this
}
const cloned = new RestockNotificationService({
manager: transactionManager,
options: this.options_,
eventBusService: this.eventBus_,
productVariantService: this.productVariantService_,
})
cloned.transactionManager_ = transactionManager
return cloned
}
async retrieve(variantId) {
const restockRepo = this.manager_.getRepository(RestockNotification)
return await restockRepo.findOne({ where: { variant_id: variantId } })
@@ -29,13 +46,13 @@ class RestockNotificationService extends BaseService {
if (existing) {
// Converting to a set handles duplicates for us
const emailSet = new Set(existing.emails)
emailSet.push(email)
emailSet.add(email)
existing.emails = Array.from(emailSet)
return await restockRepo.save(existing)
} else {
const variant = await productVariantService.retrieve(variantId)
const variant = await this.productVariantService_.retrieve(variantId)
if (variant.inventory_quantity > 0) {
throw new MedusaError(
@@ -54,15 +71,10 @@ class RestockNotificationService extends BaseService {
})
}
async delete(variantId) {
return this.atomicPhase_(async (manager) => {
const restockRepo = manager.getRepository(RestockNotification)
return restockRepo.delete(variantId)
})
}
async triggerRestock(variantId) {
return this.atomicPhase_(async (manager) => {
const restockRepo = manager.getRepository(RestockNotification)
const existing = await this.retrieve(variantId)
if (!existing) {
return
@@ -70,10 +82,13 @@ class RestockNotificationService extends BaseService {
const variant = await this.productVariantService_.retrieve(variantId)
if (variant.inventory_quantity > 0) {
await eventBus_
await this.eventBus_
.withTransaction(manager)
.emit("restock_notification.restocked")
await this.delete(variantId)
.emit("restock_notification.restocked", {
variant_id: variantId,
emails: existing.emails,
})
await restockRepo.delete(variantId)
}
})
}
@@ -1,9 +1,10 @@
class VariantSubscriber {
constructor({ eventBusService, restockNotificationService }) {
constructor({ manager, eventBusService, restockNotificationService }) {
this.manager_ = manager
this.restockNotificationService_ = restockNotificationService
eventBusService.subscribe(
"product_variant.updated",
"product-variant.updated",
this.handleVariantUpdate
)
}
@@ -11,7 +12,12 @@ class VariantSubscriber {
handleVariantUpdate = async (data) => {
const { id, fields } = data
if (fields.includes("inventory_quantity")) {
return this.restockNotificationService_.triggerRestock(id)
return await this.manager_.transaction(
async (m) =>
await this.restockNotificationService_
.withTransaction(m)
.triggerRestock(id)
)
}
}
}