Merge pull request #293 from medusajs/feat/disabling-of-notification

Feat/disabling of notification
This commit is contained in:
Sebastian Mateos Nicolajsen authored and GitHub committed 2021-07-08 09:59:09 +02:00
commit 0c96813e2f
41 files changed
+812 -66

No files matched your search

@@ -63,6 +63,9 @@ import { defaultFields, defaultRelations } from "."
* customer_id: * customer_id:
* description: The id of the customer to add on the draft order * description: The id of the customer to add on the draft order
* type: string * type: string
* no_notification_order:
* description: An optional flag passed to the resulting order to determine use of notifications.
* type: boolean
* shipping_methods: * shipping_methods:
* description: The shipping methods for the draft order * description: The shipping methods for the draft order
* type: array * type: array
@@ -123,6 +126,7 @@ export default async (req, res) => {
}) })
.optional(), .optional(),
customer_id: Validator.string().optional(), customer_id: Validator.string().optional(),
no_notification_order: Validator.boolean().optional(),
shipping_methods: Validator.array() shipping_methods: Validator.array()
.items({ .items({
option_id: Validator.string().required(), option_id: Validator.string().required(),
@@ -78,6 +78,7 @@ export const defaultFields = [
"created_at", "created_at",
"updated_at", "updated_at",
"metadata", "metadata",
"no_notification_order",
] ]
export const allowedFields = [ export const allowedFields = [
@@ -89,6 +90,7 @@ export const allowedFields = [
"created_at", "created_at",
"updated_at", "updated_at",
"metadata", "metadata",
"no_notification_order",
] ]
export const allowedRelations = ["cart"] export const allowedRelations = ["cart"]
@@ -35,6 +35,9 @@ import { defaultCartFields, defaultCartRelations, defaultFields } from "."
* code: * code:
* description: "The code that a Discount is identifed by." * description: "The code that a Discount is identifed by."
* type: string * type: string
* no_notification_order:
* description: "An optional flag passed to the resulting order to determine use of notifications."
* type: boolean
* customer_id: * customer_id:
* description: "The id of the Customer to associate the Draft Order with." * description: "The id of the Customer to associate the Draft Order with."
* type: string * type: string
@@ -68,6 +71,7 @@ export default async (req, res) => {
}) })
.optional(), .optional(),
customer_id: Validator.string().optional(), customer_id: Validator.string().optional(),
no_notification_order: Validator.boolean().optional(),
}) })
const { value, error } = schema.validate(req.body) const { value, error } = schema.validate(req.body)
@@ -88,6 +92,13 @@ export default async (req, res) => {
) )
} }
if ("no_notification_order" in value) {
await draftOrderService.update(draftOrder.id, {
no_notification_order: value.no_notification_order,
})
delete value.no_notification_order
}
await cartService.update(draftOrder.cart_id, value) await cartService.update(draftOrder.cart_id, value)
draftOrder.cart = await cartService.retrieve(draftOrder.cart_id, { draftOrder.cart = await cartService.retrieve(draftOrder.cart_id, {
@@ -42,7 +42,7 @@ describe("POST /admin/orders/:id/fulfillment", () => {
quantity: 1, quantity: 1,
}, },
], ],
undefined { metadata: undefined, no_notification: undefined }
) )
}) })
@@ -62,6 +62,7 @@ const defaultFields = [
"total", "total",
"paid_total", "paid_total",
"refundable_amount", "refundable_amount",
"no_notification",
] ]
describe("GET /admin/orders", () => { describe("GET /admin/orders", () => {
@@ -1,7 +1,7 @@
import { IdMap } from "medusa-test-utils" import { IdMap } from "medusa-test-utils"
import { request } from "../../../../../helpers/test-request" import { request } from "../../../../../helpers/test-request"
import { orders } from "../../../../../services/__mocks__/order"
import { ReturnService } from "../../../../../services/__mocks__/return" import { ReturnService } from "../../../../../services/__mocks__/return"
import { EventBusServiceMock } from "../../../../../services/__mocks__/event-bus"
describe("POST /admin/orders/:id/return", () => { describe("POST /admin/orders/:id/return", () => {
describe("successfully returns full order", () => { describe("successfully returns full order", () => {
@@ -21,6 +21,7 @@ describe("POST /admin/orders/:id/return", () => {
}, },
], ],
refund: 10, refund: 10,
no_notification: true,
}, },
adminSession: { adminSession: {
jwt: { jwt: {
@@ -47,6 +48,7 @@ describe("POST /admin/orders/:id/return", () => {
}, },
], ],
refund_amount: 10, refund_amount: 10,
no_notification: true,
shipping_method: undefined, shipping_method: undefined,
}) })
}) })
@@ -69,6 +71,7 @@ describe("POST /admin/orders/:id/return", () => {
}, },
], ],
refund: -1, refund: -1,
no_notification: true,
}, },
adminSession: { adminSession: {
jwt: { jwt: {
@@ -95,6 +98,7 @@ describe("POST /admin/orders/:id/return", () => {
}, },
], ],
refund_amount: 0, refund_amount: 0,
no_notification: true,
shipping_method: undefined, shipping_method: undefined,
}) })
}) })
@@ -118,6 +122,7 @@ describe("POST /admin/orders/:id/return", () => {
], ],
refund: -1, refund: -1,
}, },
no_notification: true,
adminSession: { adminSession: {
jwt: { jwt: {
userId: IdMap.getId("admin_user"), userId: IdMap.getId("admin_user"),
@@ -143,6 +148,7 @@ describe("POST /admin/orders/:id/return", () => {
}, },
], ],
refund_amount: 0, refund_amount: 0,
no_notification: true,
shipping_method: undefined, shipping_method: undefined,
}) })
}) })
@@ -165,6 +171,7 @@ describe("POST /admin/orders/:id/return", () => {
}, },
], ],
refund: 100, refund: 100,
no_notification: true,
return_shipping: { return_shipping: {
option_id: "opt_1234", option_id: "opt_1234",
price: 12, price: 12,
@@ -195,6 +202,7 @@ describe("POST /admin/orders/:id/return", () => {
}, },
], ],
refund_amount: 100, refund_amount: 100,
no_notification: true,
shipping_method: { shipping_method: {
option_id: "opt_1234", option_id: "opt_1234",
price: 12, price: 12,
@@ -205,4 +213,82 @@ describe("POST /admin/orders/:id/return", () => {
expect(ReturnService.fulfill).toHaveBeenCalledWith("return") expect(ReturnService.fulfill).toHaveBeenCalledWith("return")
}) })
}) })
describe("the api call overrides notification settings of order", () => {
it("eventBus is called with the proper no notification feature", async () => {
jest.clearAllMocks()
const subject = await request(
"POST",
`/admin/orders/${IdMap.getId("test-order")}/return`,
{
payload: {
items: [
{
item_id: IdMap.getId("existingLine"),
quantity: 10,
},
],
refund: 100,
return_shipping: {
option_id: "opt_1234",
price: 12,
},
no_notification: false,
},
adminSession: {
jwt: {
userId: IdMap.getId("admin_user"),
},
},
}
)
expect(EventBusServiceMock.emit).toHaveBeenCalledWith(
expect.any(String),
{
id: expect.any(String),
no_notification: false,
return_id: expect.any(String),
}
)
})
})
describe("the api call inherits notification settings of order", () => {
it("eventBus is called with the proper no notification feature", async () => {
jest.clearAllMocks()
await request(
"POST",
`/admin/orders/${IdMap.getId("test-order")}/return`,
{
payload: {
items: [
{
item_id: IdMap.getId("existingLine"),
quantity: 10,
},
],
refund: 100,
return_shipping: {
option_id: "opt_1234",
price: 12,
},
},
adminSession: {
jwt: {
userId: IdMap.getId("admin_user"),
},
},
}
)
expect(EventBusServiceMock.emit).toHaveBeenCalledWith(
expect.any(String),
{
id: expect.any(String),
no_notification: true,
return_id: expect.any(String),
}
)
})
})
}) })
@@ -91,6 +91,9 @@ import { defaultRelations, defaultFields } from "./"
* refund_amount: * refund_amount:
* description: The amount to refund the Customer when the Claim type is `refund`. * description: The amount to refund the Customer when the Claim type is `refund`.
* type: integer * type: integer
* no_notification:
* description: If set to true no notification will be send related to this Claim.
* type: boolean
* metadata: * metadata:
* description: An optional set of key-value pairs to hold additional information. * description: An optional set of key-value pairs to hold additional information.
* type: object * type: object
@@ -108,7 +111,6 @@ import { defaultRelations, defaultFields } from "./"
*/ */
export default async (req, res) => { export default async (req, res) => {
const { id } = req.params const { id } = req.params
const schema = Validator.object().keys({ const schema = Validator.object().keys({
type: Validator.string() type: Validator.string()
.valid("replace", "refund") .valid("replace", "refund")
@@ -155,6 +157,7 @@ export default async (req, res) => {
.integer() .integer()
.optional(), .optional(),
shipping_address: Validator.object().optional(), shipping_address: Validator.object().optional(),
no_notification: Validator.boolean().optional(),
metadata: Validator.object().optional(), metadata: Validator.object().optional(),
}) })
@@ -162,7 +165,6 @@ export default async (req, res) => {
if (error) { if (error) {
throw new MedusaError(MedusaError.Types.INVALID_DATA, error.details) throw new MedusaError(MedusaError.Types.INVALID_DATA, error.details)
} }
const idempotencyKeyService = req.scope.resolve("idempotencyKeyService") const idempotencyKeyService = req.scope.resolve("idempotencyKeyService")
const headerKey = req.get("Idempotency-Key") || "" const headerKey = req.get("Idempotency-Key") || ""
@@ -212,6 +214,7 @@ export default async (req, res) => {
return_shipping: value.return_shipping, return_shipping: value.return_shipping,
additional_items: value.additional_items, additional_items: value.additional_items,
shipping_methods: value.shipping_methods, shipping_methods: value.shipping_methods,
no_notification: value.no_notification,
metadata: value.metadata, metadata: value.metadata,
}) })
@@ -24,6 +24,9 @@ import { defaultRelations, defaultFields } from "./"
* quantity: * quantity:
* description: The quantity of the Line Item to fulfill. * description: The quantity of the Line Item to fulfill.
* type: integer * type: integer
* no_notification:
* description: If set to true no notification will be send related to this Swap.
* type: boolean
* metadata: * metadata:
* description: An optional set of key-value pairs to hold additional information. * description: An optional set of key-value pairs to hold additional information.
* type: object * type: object
@@ -49,6 +52,7 @@ export default async (req, res) => {
quantity: Validator.number().required(), quantity: Validator.number().required(),
}) })
.required(), .required(),
no_notification: Validator.boolean().optional(),
metadata: Validator.object().optional(), metadata: Validator.object().optional(),
}) })
@@ -60,7 +64,10 @@ export default async (req, res) => {
try { try {
const orderService = req.scope.resolve("orderService") const orderService = req.scope.resolve("orderService")
await orderService.createFulfillment(id, value.items, value.metadata) await orderService.createFulfillment(id, value.items, {
metadata: value.metadata,
no_notification: value.no_notification,
})
const order = await orderService.retrieve(id, { const order = await orderService.retrieve(id, {
select: defaultFields, select: defaultFields,
@@ -27,6 +27,7 @@ export default async (req, res) => {
items: Validator.array().optional(), items: Validator.array().optional(),
}) })
.required(), .required(),
no_notification: Validator.boolean().optional(),
metadata: Validator.object().optional(), metadata: Validator.object().optional(),
}) })
@@ -21,6 +21,9 @@ import { defaultRelations, defaultFields } from "./"
* type: array * type: array
* items: * items:
* type: string * type: string
* no_notification:
* description: If set to true no notification will be send related to this Shipment.
* type: boolean
* tags: * tags:
* - Order * - Order
* responses: * responses:
@@ -41,6 +44,7 @@ export default async (req, res) => {
tracking_numbers: Validator.array() tracking_numbers: Validator.array()
.items(Validator.string()) .items(Validator.string())
.optional(), .optional(),
no_notification: Validator.boolean().optional(),
}) })
const { value, error } = schema.validate(req.body) const { value, error } = schema.validate(req.body)
@@ -54,7 +58,8 @@ export default async (req, res) => {
await orderService.createShipment( await orderService.createShipment(
id, id,
value.fulfillment_id, value.fulfillment_id,
value.tracking_numbers.map(n => ({ tracking_number: n })) value.tracking_numbers.map(n => ({ tracking_number: n })),
{ no_notification: value.no_notification }
) )
const order = await orderService.retrieve(id, { const order = await orderService.retrieve(id, {
@@ -22,6 +22,9 @@ import { defaultFields, defaultRelations } from "./"
* type: array * type: array
* items: * items:
* type: string * type: string
* no_notification:
* description: If set to true no notification will be send related to this Claim.
* type: boolean
* tags: * tags:
* - Order * - Order
* responses: * responses:
@@ -42,6 +45,7 @@ export default async (req, res) => {
tracking_numbers: Validator.array() tracking_numbers: Validator.array()
.items(Validator.string()) .items(Validator.string())
.optional(), .optional(),
no_notification: Validator.boolean().optional(),
}) })
const { value, error } = schema.validate(req.body) const { value, error } = schema.validate(req.body)
@@ -56,7 +60,8 @@ export default async (req, res) => {
await swapService.createShipment( await swapService.createShipment(
swap_id, swap_id,
value.fulfillment_id, value.fulfillment_id,
value.tracking_numbers.map(n => ({ tracking_number: n })) value.tracking_numbers.map(n => ({ tracking_number: n })),
{ no_notification: value.no_notification }
) )
const order = await orderService.retrieve(id, { const order = await orderService.retrieve(id, {
@@ -45,6 +45,9 @@ import { defaultFields, defaultRelations } from "./"
* quantity: * quantity:
* description: The quantity of the Product Variant to ship. * description: The quantity of the Product Variant to ship.
* type: integer * type: integer
* no_notification:
* description: If set to true no notification will be send related to this Swap.
* type: boolean
* tags: * tags:
* - Order * - Order
* responses: * responses:
@@ -79,6 +82,7 @@ export default async (req, res) => {
variant_id: Validator.string().required(), variant_id: Validator.string().required(),
quantity: Validator.number().required(), quantity: Validator.number().required(),
}), }),
no_notification: Validator.boolean().optional(),
}) })
const { value, error } = schema.validate(req.body) const { value, error } = schema.validate(req.body)
@@ -134,7 +138,10 @@ export default async (req, res) => {
value.return_items, value.return_items,
value.additional_items, value.additional_items,
value.return_shipping, value.return_shipping,
{ idempotency_key: idempotencyKey.idempotency_key } {
idempotency_key: idempotencyKey.idempotency_key,
no_notification: value.no_notification,
}
) )
await swapService.withTransaction(manager).createCart(swap.id) await swapService.withTransaction(manager).createCart(swap.id)
@@ -17,6 +17,9 @@ import { defaultRelations, defaultFields } from "./"
* metadata: * metadata:
* description: An optional set of key-value pairs to hold additional information. * description: An optional set of key-value pairs to hold additional information.
* type: object * type: object
* no_notification:
* description: If set to true no notification will be send related to this Claim.
* type: boolean
* tags: * tags:
* - Order * - Order
* responses: * responses:
@@ -34,6 +37,7 @@ export default async (req, res) => {
const schema = Validator.object().keys({ const schema = Validator.object().keys({
metadata: Validator.object().optional(), metadata: Validator.object().optional(),
no_notification: Validator.boolean().optional(),
}) })
const { value, error } = schema.validate(req.body) const { value, error } = schema.validate(req.body)
@@ -47,9 +51,10 @@ export default async (req, res) => {
const entityManager = req.scope.resolve("manager") const entityManager = req.scope.resolve("manager")
await entityManager.transaction(async manager => { await entityManager.transaction(async manager => {
await claimService await claimService.withTransaction(manager).createFulfillment(claim_id, {
.withTransaction(manager) metadata: value.metadata,
.createFulfillment(claim_id, value.metadata) no_notification: value.no_notification,
})
}) })
const order = await orderService.retrieve(id, { const order = await orderService.retrieve(id, {
@@ -17,6 +17,9 @@ import { defaultRelations, defaultFields } from "./"
* metadata: * metadata:
* description: An optional set of key-value pairs to hold additional information. * description: An optional set of key-value pairs to hold additional information.
* type: object * type: object
* no_notification:
* description: If set to true no notification will be send related to this Claim.
* type: boolean
* tags: * tags:
* - Order * - Order
* responses: * responses:
@@ -34,6 +37,7 @@ export default async (req, res) => {
const schema = Validator.object().keys({ const schema = Validator.object().keys({
metadata: Validator.object().optional(), metadata: Validator.object().optional(),
no_notification: Validator.boolean().optional,
}) })
const { value, error } = schema.validate(req.body) const { value, error } = schema.validate(req.body)
@@ -47,9 +51,10 @@ export default async (req, res) => {
const entityManager = req.scope.resolve("manager") const entityManager = req.scope.resolve("manager")
await entityManager.transaction(async manager => { await entityManager.transaction(async manager => {
await swapService await swapService.withTransaction(manager).createFulfillment(swap_id, {
.withTransaction(manager) metadata: value.metadata,
.createFulfillment(swap_id, value.metadata) no_notification: value.no_notification,
})
const order = await orderService.withTransaction(manager).retrieve(id, { const order = await orderService.withTransaction(manager).retrieve(id, {
select: defaultFields, select: defaultFields,
@@ -237,6 +237,7 @@ export const defaultFields = [
"total", "total",
"paid_total", "paid_total",
"refundable_amount", "refundable_amount",
"no_notification",
] ]
export const allowedFields = [ export const allowedFields = [
@@ -265,6 +266,7 @@ export const allowedFields = [
"total", "total",
"paid_total", "paid_total",
"refundable_amount", "refundable_amount",
"no_notification",
] ]
export const allowedRelations = [ export const allowedRelations = [
@@ -25,6 +25,9 @@ import { defaultRelations, defaultFields } from "./"
* note: * note:
* description: A not with additional details about the Refund. * description: A not with additional details about the Refund.
* type: string * type: string
* no_notification:
* description: If set to true no notification will be send related to this Refund.
* type: boolean
* tags: * tags:
* - Order * - Order
* responses: * responses:
@@ -47,9 +50,11 @@ export default async (req, res) => {
note: Validator.string() note: Validator.string()
.allow("") .allow("")
.optional(), .optional(),
no_notification: Validator.boolean().optional(),
}) })
const { value, error } = schema.validate(req.body) const { value, error } = schema.validate(req.body)
if (error) { if (error) {
throw new MedusaError(MedusaError.Types.INVALID_DATA, error.details) throw new MedusaError(MedusaError.Types.INVALID_DATA, error.details)
} }
@@ -57,7 +62,13 @@ export default async (req, res) => {
try { try {
const orderService = req.scope.resolve("orderService") const orderService = req.scope.resolve("orderService")
await orderService.createRefund(id, value.amount, value.reason, value.note) await orderService.createRefund(
id,
value.amount,
value.reason,
value.note,
{ no_notification: value.no_notification }
)
const order = await orderService.retrieve(id, { const order = await orderService.retrieve(id, {
select: defaultFields, select: defaultFields,
@@ -43,6 +43,9 @@ import { defaultRelations, defaultFields } from "./"
* receive_now: * receive_now:
* description: A flag to indicate if the Return should be registerd as received immediately. * description: A flag to indicate if the Return should be registerd as received immediately.
* type: boolean * type: boolean
* no_notification:
* description: A flag to indicate if no notifications should be emitted related to the requested Return.
* type: boolean
* refund: * refund:
* description: The amount to refund. * description: The amount to refund.
* type: integer * type: integer
@@ -79,6 +82,7 @@ export default async (req, res) => {
}) })
.optional(), .optional(),
receive_now: Validator.boolean().default(false), receive_now: Validator.boolean().default(false),
no_notification: Validator.boolean().optional(),
refund: Validator.number() refund: Validator.number()
.integer() .integer()
.optional(), .optional(),
@@ -141,6 +145,13 @@ export default async (req, res) => {
} }
} }
let order = await orderService
.withTransaction(manager)
.retrieve(id)
const evaluatedNoNotification = value.no_notification !== undefined ? value.no_notification : order.no_notification
returnObj.no_notification = evaluatedNoNotification
const createdReturn = await returnService const createdReturn = await returnService
.withTransaction(manager) .withTransaction(manager)
.create(returnObj) .create(returnObj)
@@ -156,6 +167,7 @@ export default async (req, res) => {
.emit("order.return_requested", { .emit("order.return_requested", {
id, id,
return_id: createdReturn.id, return_id: createdReturn.id,
no_notification: evaluatedNoNotification
}) })
return { return {
@@ -62,6 +62,9 @@ import { defaultRelations, defaultFields } from "./"
* price: * price:
* description: The price to charge for the Shipping Method * description: The price to charge for the Shipping Method
* type: integer * type: integer
* no_notification:
* description: If set to true no notification will be send related to this Swap.
* type: boolean
* metadata: * metadata:
* description: An optional set of key-value pairs to hold additional information. * description: An optional set of key-value pairs to hold additional information.
* type: object * type: object
@@ -106,6 +109,7 @@ export default async (req, res) => {
.optional(), .optional(),
}) })
.optional(), .optional(),
no_notification: Validator.boolean().optional(),
metadata: Validator.object().optional(), metadata: Validator.object().optional(),
}) })
@@ -23,6 +23,7 @@ export default async (req, res) => {
data: Validator.object(), data: Validator.object(),
items: Validator.array(), items: Validator.array(),
}), }),
no_notification: Validator.boolean(),
}) })
const { value, error } = schema.validate(req.body) const { value, error } = schema.validate(req.body)
@@ -54,6 +54,7 @@ export default async (req, res) => {
}) })
const { value, error } = schema.validate(req.body) const { value, error } = schema.validate(req.body)
if (error) { if (error) {
throw new MedusaError(MedusaError.Types.INVALID_DATA, error.details) throw new MedusaError(MedusaError.Types.INVALID_DATA, error.details)
} }
+1
View File
@@ -43,3 +43,4 @@ export { Swap } from "./models/swap"
export { User } from "./models/user" export { User } from "./models/user"
export { DraftOrder } from "./models/draft-order" export { DraftOrder } from "./models/draft-order"
export { ReturnReason } from "./models/return-reason" export { ReturnReason } from "./models/return-reason"
@@ -0,0 +1,25 @@
import {MigrationInterface, QueryRunner} from "typeorm";
export class noNotification1623231564533 implements MigrationInterface {
name = 'noNotification1623231564533'
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "return" ADD "no_notification" boolean`);
await queryRunner.query(`ALTER TABLE "claim_order" ADD "no_notification" boolean`);
await queryRunner.query(`ALTER TABLE "swap" ADD "no_notification" boolean`);
await queryRunner.query(`ALTER TABLE "order" ADD "no_notification" boolean`);
await queryRunner.query(`ALTER TABLE "draft_order" ADD "no_notification_order" boolean`);
await queryRunner.query(`ALTER TABLE "fulfillment" ADD "no_notification" boolean`);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "fulfillment" DROP COLUMN "no_notification"`);
await queryRunner.query(`ALTER TABLE "draft_order" DROP COLUMN "no_notification_order"`);
await queryRunner.query(`ALTER TABLE "order" DROP COLUMN "no_notification"`);
await queryRunner.query(`ALTER TABLE "swap" DROP COLUMN "no_notification"`);
await queryRunner.query(`ALTER TABLE "claim_order" DROP COLUMN "no_notification"`);
await queryRunner.query(`ALTER TABLE "return" DROP COLUMN "no_notification"`);
}
}
@@ -134,6 +134,9 @@ export class ClaimOrder {
@DeleteDateColumn({ type: "timestamptz" }) @DeleteDateColumn({ type: "timestamptz" })
deleted_at: Date deleted_at: Date
@Column({ type: "boolean", nullable: true})
no_notification: Boolean
@Column({ type: "jsonb", nullable: true }) @Column({ type: "jsonb", nullable: true })
metadata: any metadata: any
@@ -223,6 +226,9 @@ export class ClaimOrder {
* deleted_at: * deleted_at:
* type: string * type: string
* format: date-time * format: date-time
* no_notification:
* description: "Flag for describing whether or not notifications related to this should be send."
* type: boolean
* metadata: * metadata:
* type: object * type: object
*/ */
@@ -61,6 +61,9 @@ export class DraftOrder {
@Column({ type: "timestamptz", nullable: true }) @Column({ type: "timestamptz", nullable: true })
completed_at: Date completed_at: Date
@Column({ nullable: true})
no_notification_order: boolean
@Column({ type: "jsonb", nullable: true }) @Column({ type: "jsonb", nullable: true })
metadata: any metadata: any
@@ -115,6 +118,8 @@ export class DraftOrder {
* completed_at: * completed_at:
* type: string * type: string
* format: date-time * format: date-time
* no_notification_order:
* type: boolean
* metadata: * metadata:
* type: object * type: object
* idempotency_key: * idempotency_key:
@@ -62,6 +62,9 @@ export class Fulfillment {
@JoinColumn({ name: "order_id" }) @JoinColumn({ name: "order_id" })
order: Order order: Order
@Column({ type: "boolean", nullable: true})
no_notification: Boolean
@Index() @Index()
@Column() @Column()
provider_id: string provider_id: string
@@ -157,6 +160,9 @@ export class Fulfillment {
* description: "The date with timezone at which the Fulfillment was shipped." * description: "The date with timezone at which the Fulfillment was shipped."
* type: string * type: string
* format: date-time * format: date-time
* no_notification:
* description: "Flag for describing whether or not notifications related to this should be send."
* type: boolean
* canceled_at: * canceled_at:
* description: "The date with timezone at which the Fulfillment was canceled." * description: "The date with timezone at which the Fulfillment was canceled."
* type: string * type: string
+6
View File
@@ -245,6 +245,9 @@ export class Order {
@Column({ type: "jsonb", nullable: true }) @Column({ type: "jsonb", nullable: true })
metadata: any metadata: any
@Column({ type: "boolean", nullable: true})
no_notification: Boolean
@Column({ nullable: true }) @Column({ nullable: true })
idempotency_key: string idempotency_key: string
@@ -414,4 +417,7 @@ export class Order {
* type: integer * type: integer
* paid_total: * paid_total:
* type: integer * type: integer
* no_notification:
* description: "Flag for describing whether or not notifications related to this should be send."
* type: boolean
*/ */
+6
View File
@@ -99,6 +99,9 @@ export class Return {
@UpdateDateColumn({ type: "timestamptz" }) @UpdateDateColumn({ type: "timestamptz" })
updated_at: Date updated_at: Date
@Column({ type: "boolean", nullable: true})
no_notification: Boolean
@Column({ type: "jsonb", nullable: true }) @Column({ type: "jsonb", nullable: true })
metadata: any metadata: any
@@ -165,6 +168,9 @@ export class Return {
* description: "The date with timezone at which the resource was last updated." * description: "The date with timezone at which the resource was last updated."
* type: string * type: string
* format: date-time * format: date-time
* no_notification:
* description: "When set to true, no notification will be sent related to this return."
* type: boolean
* metadata: * metadata:
* description: "An optional key-value map with additional information." * description: "An optional key-value map with additional information."
* type: object * type: object
+6
View File
@@ -130,6 +130,9 @@ export class Swap {
@DeleteDateColumn({ type: "timestamptz" }) @DeleteDateColumn({ type: "timestamptz" })
deleted_at: Date deleted_at: Date
@Column({ type: "boolean", nullable: true})
no_notification: Boolean
@Column({ type: "jsonb", nullable: true }) @Column({ type: "jsonb", nullable: true })
metadata: any metadata: any
@@ -224,6 +227,9 @@ export class Swap {
* description: "The date with timezone at which the resource was last updated." * description: "The date with timezone at which the resource was last updated."
* type: string * type: string
* format: date-time * format: date-time
* no_notification:
* description: "If set to true, no notification will be sent related to this swap"
* type: boolean
* metadata: * metadata:
* description: "An optional key-value map with additional information." * description: "An optional key-value map with additional information."
* type: object * type: object
@@ -47,6 +47,7 @@ export const orders = {
providerid: "default_provider", providerid: "default_provider",
data: {}, data: {},
}, },
no_notification: true,
shipping_method: [ shipping_method: [
{ {
providerid: "default_provider", providerid: "default_provider",
@@ -99,6 +100,7 @@ export const orders = {
payment_method: { payment_method: {
providerid: "default_provider", providerid: "default_provider",
}, },
no_notification: false,
shipping_methods: [ shipping_methods: [
{ {
id: IdMap.getId("expensiveShipping"), id: IdMap.getId("expensiveShipping"),
@@ -22,6 +22,7 @@ describe("ClaimService", () => {
order: { order: {
id: "1234", id: "1234",
region_id: "order_region", region_id: "order_region",
no_notification: true,
items: [ items: [
{ {
id: "itm_1", id: "itm_1",
@@ -113,6 +114,7 @@ describe("ClaimService", () => {
quantity: 1, quantity: 1,
}, },
], ],
no_notification: true,
}) })
expect(withTransactionMock).toHaveBeenCalledWith("lineItem") expect(withTransactionMock).toHaveBeenCalledWith("lineItem")
@@ -138,6 +140,7 @@ describe("ClaimService", () => {
expect(claimRepo.create).toHaveBeenCalledTimes(1) expect(claimRepo.create).toHaveBeenCalledTimes(1)
expect(claimRepo.create).toHaveBeenCalledWith({ expect(claimRepo.create).toHaveBeenCalledWith({
payment_status: "not_refunded", payment_status: "not_refunded",
no_notification: true,
refund_amount: 1000, refund_amount: 1000,
type: "refund", type: "refund",
order_id: "1234", order_id: "1234",
@@ -156,6 +159,7 @@ describe("ClaimService", () => {
expect(eventBusService.emit).toHaveBeenCalledTimes(1) expect(eventBusService.emit).toHaveBeenCalledTimes(1)
expect(eventBusService.emit).toHaveBeenCalledWith("claim.created", { expect(eventBusService.emit).toHaveBeenCalledWith("claim.created", {
id: "claim_134", id: "claim_134",
no_notification: true,
}) })
}) })
@@ -208,6 +212,25 @@ describe("ClaimService", () => {
}) })
).rejects.toThrow(`Claims must have at least one claim item.`) ).rejects.toThrow(`Claims must have at least one claim item.`)
}) })
it.each(
[
[false, false],
[undefined, true],
],
"passes correct no_notification status to event bus",
async (input, expected) => {
await claimService.create({
...testClaim,
no_notification: input,
})
expect(eventBusService.emit).toHaveBeenCalledWith(expect.any(String), {
id: expect.any(String),
no_notification: expected,
})
}
)
}) })
describe("retrieve", () => { describe("retrieve", () => {
@@ -294,7 +317,9 @@ describe("ClaimService", () => {
}) })
it("successfully creates fulfillment", async () => { it("successfully creates fulfillment", async () => {
await claimService.createFulfillment("claim_id", { meta: "data" }) await claimService.createFulfillment("claim_id", {
metadata: { meta: "data" },
})
expect(withTransactionMock).toHaveBeenCalledTimes(3) expect(withTransactionMock).toHaveBeenCalledTimes(3)
expect(withTransactionMock).toHaveBeenCalledWith("eventBus") expect(withTransactionMock).toHaveBeenCalledWith("eventBus")
@@ -414,7 +439,10 @@ describe("ClaimService", () => {
) )
await claimService.createShipment("claim", "ful_123", ["track1234"], { await claimService.createShipment("claim", "ful_123", ["track1234"], {
meta: "data", metadata: {
meta: "data",
},
no_notification: false,
}) })
expect(withTransactionMock).toHaveBeenCalledTimes(3) expect(withTransactionMock).toHaveBeenCalledTimes(3)
@@ -426,7 +454,12 @@ describe("ClaimService", () => {
expect(fulfillmentService.createShipment).toHaveBeenCalledWith( expect(fulfillmentService.createShipment).toHaveBeenCalledWith(
"ful_123", "ful_123",
["track1234"], ["track1234"],
{ meta: "data" } {
metadata: {
meta: "data",
},
no_notification: false,
}
) )
expect(lineItemService.update).toHaveBeenCalledTimes(1) expect(lineItemService.update).toHaveBeenCalledTimes(1)
@@ -1,5 +1,6 @@
import _ from "lodash" import _ from "lodash"
import { IdMap, MockRepository, MockManager } from "medusa-test-utils" import { MockRepository, MockManager } from "medusa-test-utils"
import { EventBusServiceMock } from "../__mocks__/event-bus"
import DraftOrderService from "../draft-order" import DraftOrderService from "../draft-order"
const eventBusService = { const eventBusService = {
@@ -205,4 +206,93 @@ describe("DraftOrderService", () => {
} }
}) })
}) })
describe("update", () => {
const testOrder = {
region_id: "test-region",
shipping_address_id: "test-shipping",
billing_address_id: "test-billing",
customer_id: "test-customer",
items: [{ variant_id: "test-variant", quantity: 2, metadata: {} }],
shipping_methods: [
{
option_id: "test-option",
data: {},
},
],
}
const completedOrder = {
status: "completed",
...testOrder,
}
const draftOrderRepository = MockRepository({
create: d => ({
...d,
}),
save: d => ({
id: "test-draft-order",
...d,
}),
findOne: q => {
switch (q.where.id) {
case "completed":
return Promise.resolve(completedOrder)
default:
return Promise.resolve(testOrder)
}
},
})
const draftOrderService = new DraftOrderService({
manager: MockManager,
regionService: undefined,
cartService: undefined,
shippingOptionService: undefined,
lineItemService: undefined,
productVariantService: undefined,
draftOrderRepository,
addressRepository: undefined,
eventBusService: EventBusServiceMock,
})
beforeEach(async () => {
jest.clearAllMocks()
})
it("calls draftOrder model functions", async () => {
await draftOrderService.update("test-draft-order", {
no_notification_order: true,
})
expect(draftOrderRepository.save).toHaveBeenCalledTimes(1)
expect(draftOrderRepository.save).toHaveBeenCalledWith({
no_notification_order: true,
billing_address_id: "test-billing",
customer_id: "test-customer",
items: [
{
metadata: {},
quantity: 2,
variant_id: "test-variant",
},
],
region_id: "test-region",
shipping_address_id: "test-shipping",
shipping_methods: [
{
data: {},
option_id: "test-option",
},
],
})
})
it("fails to update draftOrder when already complete", async () => {
await expect(draftOrderService.update("completed", {})).rejects.toThrow(
"Can't update a draft order which is complete"
)
})
})
}) })
@@ -1,30 +1,31 @@
import NotificationService from "../notification" import NotificationService from "../notification"
import { IdMap, MockManager, MockRepository } from "medusa-test-utils" import { MockManager, MockRepository } from "medusa-test-utils"
describe("NotificationService", () => { describe("NotificationService", () => {
describe("send", () => { const notificationRepository = MockRepository({ create: c => c })
const notificationRepository = MockRepository({ create: c => c })
const container = { const container = {
manager: MockManager, manager: MockManager,
notificationRepository, notificationRepository,
noti_test: { noti_test: {
sendNotification: jest.fn(() => sendNotification: jest.fn(() =>
Promise.resolve({ Promise.resolve({
to: "test@mail.com", to: "test@mail.com",
data: { id: "something" }, data: { id: "something" },
}) })
), ),
}, },
} }
const notificationService = new NotificationService(container) beforeEach(() => {
jest.clearAllMocks()
})
beforeEach(() => { describe("send", () =>{
jest.clearAllMocks()
})
it("successfully calls provider and saves noti", async () => { it("successfully calls provider and saves noti", async () => {
const notificationService = new NotificationService(container)
await notificationService.send("event.test", { id: "test" }, "test") await notificationService.send("event.test", { id: "test" }, "test")
expect(container.noti_test.sendNotification).toHaveBeenCalledTimes(1) expect(container.noti_test.sendNotification).toHaveBeenCalledTimes(1)
@@ -51,4 +52,30 @@ describe("NotificationService", () => {
expect(notificationRepository.save).toHaveBeenCalledWith(constructed) expect(notificationRepository.save).toHaveBeenCalledWith(constructed)
}) })
}) })
describe("handleEvent", () => {
it("cancels notification if no_notification is set", async () => {
const notificationService = new NotificationService(container)
const event = "event.test"
notificationService.subscribe(event, "test")
await notificationService.handleEvent(event, {id: "id",
return_id: "id",
no_notification: true})
expect(container.noti_test.sendNotification).not.toHaveBeenCalled()
})
it("if no_notification is not set notification is send", async () => {
const notificationService = new NotificationService(container)
const event = "event.test"
notificationService.subscribe(event, "test")
await notificationService.handleEvent(event, {id: "id", return_id: "id"})
expect(container.noti_test.sendNotification).toHaveBeenCalledTimes(1)
})
})
}) })
@@ -711,6 +711,7 @@ describe("OrderService", () => {
const order = { const order = {
fulfillments: [], fulfillments: [],
shipping_methods: [{ id: "ship" }], shipping_methods: [{ id: "ship" }],
no_notification: true,
items: [ items: [
{ {
id: "item_1", id: "item_1",
@@ -859,6 +860,31 @@ describe("OrderService", () => {
fulfillment_status: "partially_fulfilled", fulfillment_status: "partially_fulfilled",
}) })
}) })
it.each([
[true, true],
[false, false],
[undefined, true],
])(
"emits correct no_notification option with '%s'",
async (input, expected) => {
await orderService.createFulfillment(
"test-order",
[
{
item_id: "item_1",
quantity: 1,
},
],
{ no_notification: input }
)
expect(eventBusService.emit).toHaveBeenCalledWith(expect.any(String), {
id: expect.any(String),
no_notification: expected,
})
}
)
}) })
describe("registerReturnReceived", () => { describe("registerReturnReceived", () => {
@@ -975,6 +1001,7 @@ describe("OrderService", () => {
fulfilled_quantity: 0, fulfilled_quantity: 0,
}, },
], ],
no_notification: true,
} }
const orderRepo = MockRepository({ const orderRepo = MockRepository({
@@ -996,7 +1023,11 @@ describe("OrderService", () => {
} }
const fulfillmentService = { const fulfillmentService = {
retrieve: () => Promise.resolve({ order_id: IdMap.getId("test") }), retrieve: () =>
Promise.resolve({
order_id: IdMap.getId("test"),
no_notification: true,
}),
createShipment: jest createShipment: jest
.fn() .fn()
.mockImplementation((shipmentId, tracking, meta) => { .mockImplementation((shipmentId, tracking, meta) => {
@@ -1036,10 +1067,12 @@ describe("OrderService", () => {
) )
expect(fulfillmentService.createShipment).toHaveBeenCalledTimes(1) expect(fulfillmentService.createShipment).toHaveBeenCalledTimes(1)
expect(fulfillmentService.createShipment).toHaveBeenCalledWith( expect(
fulfillmentService.createShipment
).toHaveBeenCalledWith(
IdMap.getId("fulfillment"), IdMap.getId("fulfillment"),
[{ tracking_number: "1234" }, { tracking_number: "2345" }], [{ tracking_number: "1234" }, { tracking_number: "2345" }],
{} { metadata: undefined, no_notification: true }
) )
expect(orderRepo.save).toHaveBeenCalledTimes(1) expect(orderRepo.save).toHaveBeenCalledTimes(1)
@@ -1048,6 +1081,27 @@ describe("OrderService", () => {
fulfillment_status: "shipped", fulfillment_status: "shipped",
}) })
}) })
it.each([
[true, true],
[false, false],
[undefined, true],
])(
"emits correct no_notification option with '%s'",
async (input, expected) => {
await orderService.createShipment(
IdMap.getId("test"),
IdMap.getId("fulfillment"),
[{ tracking_number: "1234" }, { tracking_number: "2345" }],
{ no_notification: input }
)
expect(eventBusService.emit).toHaveBeenCalledWith(expect.any(String), {
id: expect.any(String),
no_notification: expected,
})
}
)
}) })
describe("createRefund", () => { describe("createRefund", () => {
@@ -1081,6 +1135,7 @@ describe("OrderService", () => {
paid_total: 100, paid_total: 100,
refundable_amount: 100, refundable_amount: 100,
refunded_total: 0, refunded_total: 0,
no_notification: true,
}) })
}, },
}) })
@@ -1129,5 +1184,27 @@ describe("OrderService", () => {
) )
).rejects.toThrow("Cannot refund more than the original order amount") ).rejects.toThrow("Cannot refund more than the original order amount")
}) })
it.each([
[false, false],
[undefined, true],
])(
"emits correct no_notification option with '%s'",
async (input, expected) => {
await orderService.createRefund(
IdMap.getId("order_123"),
100,
"discount",
"note",
{ no_notification: input }
)
expect(eventBusService.emit).toHaveBeenCalledWith(expect.any(String), {
id: expect.any(String),
no_notification: expected,
refund_id: expect.any(String),
})
}
)
}) })
}) })
@@ -57,6 +57,7 @@ const testOrder = generateOrder(
currency_code: "dkk", currency_code: "dkk",
region_id: IdMap.getId("region"), region_id: IdMap.getId("region"),
tax_rate: 0, tax_rate: 0,
no_notification: true,
shipping_address: { shipping_address: {
first_name: "test", first_name: "test",
last_name: "testson", last_name: "testson",
@@ -329,6 +330,7 @@ describe("SwapService", () => {
order_id: IdMap.getId("test"), order_id: IdMap.getId("test"),
fulfillment_status: "not_fulfilled", fulfillment_status: "not_fulfilled",
payment_status: "not_paid", payment_status: "not_paid",
no_notification: true,
additional_items: [ additional_items: [
{ {
unit_price: 100, unit_price: 100,
@@ -340,6 +342,31 @@ describe("SwapService", () => {
expect(returnService.create).toHaveBeenCalledTimes(1) expect(returnService.create).toHaveBeenCalledTimes(1)
}) })
it.each([
[true, true],
[false, false],
[undefined, true],
])(
"passes correct no_notification to eventBus with %s",
async (input, expected) => {
await swapService.create(
testOrder,
[{ item_id: IdMap.getId("line"), quantity: 1 }],
[{ variant_id: IdMap.getId("new-variant"), quantity: 1 }],
{
id: IdMap.getId("return-shipping"),
price: 20,
},
{ no_notification: input }
)
expect(eventBusService.emit).toHaveBeenCalledWith(
expect.any(String),
{ id: undefined, no_notification: expected }
)
}
)
}) })
}) })
+49 -4
View File
@@ -101,7 +101,7 @@ class ClaimService extends BaseService {
const claimRepo = manager.getCustomRepository(this.claimRepository_) const claimRepo = manager.getCustomRepository(this.claimRepository_)
const claim = await this.retrieve(id, { relations: ["shipping_methods"] }) const claim = await this.retrieve(id, { relations: ["shipping_methods"] })
const { claim_items, shipping_methods, metadata } = data const { claim_items, shipping_methods, metadata, no_notification } = data
if (metadata) { if (metadata) {
claim.metadata = this.setMetadata_(claim, metadata) claim.metadata = this.setMetadata_(claim, metadata)
@@ -135,6 +135,11 @@ class ClaimService extends BaseService {
} }
} }
if (no_notification !== undefined) {
claim.no_notification = no_notification
await claimRepo.save(claim)
}
if (claim_items) { if (claim_items) {
for (const i of claim_items) { for (const i of claim_items) {
if (i.id) { if (i.id) {
@@ -149,6 +154,7 @@ class ClaimService extends BaseService {
.withTransaction(manager) .withTransaction(manager)
.emit(ClaimService.Events.UPDATED, { .emit(ClaimService.Events.UPDATED, {
id: claim.id, id: claim.id,
no_notification: claim.no_notification,
}) })
return claim return claim
@@ -174,6 +180,7 @@ class ClaimService extends BaseService {
refund_amount, refund_amount,
shipping_address, shipping_address,
shipping_address_id, shipping_address_id,
no_notification,
...rest ...rest
} = data } = data
@@ -233,6 +240,9 @@ class ClaimService extends BaseService {
) )
) )
const evaluatedNoNotification =
no_notification !== undefined ? no_notification : order.no_notification
const created = claimRepo.create({ const created = claimRepo.create({
shipping_address_id: addressId, shipping_address_id: addressId,
payment_status: type === "refund" ? "not_refunded" : "na", payment_status: type === "refund" ? "not_refunded" : "na",
@@ -241,6 +251,7 @@ class ClaimService extends BaseService {
type, type,
additional_items: newItems, additional_items: newItems,
order_id: order.id, order_id: order.id,
no_notification: evaluatedNoNotification,
}) })
const result = await claimRepo.save(created) const result = await claimRepo.save(created)
@@ -281,6 +292,7 @@ class ClaimService extends BaseService {
metadata: ci.metadata, metadata: ci.metadata,
})), })),
shipping_method: return_shipping, shipping_method: return_shipping,
no_notification: evaluatedNoNotification,
}) })
} }
@@ -288,13 +300,22 @@ class ClaimService extends BaseService {
.withTransaction(manager) .withTransaction(manager)
.emit(ClaimService.Events.CREATED, { .emit(ClaimService.Events.CREATED, {
id: result.id, id: result.id,
no_notification: result.no_notification,
}) })
return result return result
}) })
} }
createFulfillment(id, metadata = {}) { createFulfillment(
id,
config = {
metadata: {},
no_notification: undefined,
}
) {
const { metadata, no_notification } = config
return this.atomicPhase_(async manager => { return this.atomicPhase_(async manager => {
const claim = await this.retrieve(id, { const claim = await this.retrieve(id, {
relations: [ relations: [
@@ -331,6 +352,9 @@ class ClaimService extends BaseService {
) )
} }
const evaluatedNoNotification =
no_notification !== undefined ? no_notification : claim.no_notification
const fulfillments = await this.fulfillmentService_ const fulfillments = await this.fulfillmentService_
.withTransaction(manager) .withTransaction(manager)
.createFulfillment( .createFulfillment(
@@ -347,6 +371,7 @@ class ClaimService extends BaseService {
items: claim.additional_items, items: claim.additional_items,
shipping_methods: claim.shipping_methods, shipping_methods: claim.shipping_methods,
is_claim: true, is_claim: true,
no_notification: evaluatedNoNotification,
}, },
claim.additional_items.map(i => ({ claim.additional_items.map(i => ({
item_id: i.id, item_id: i.id,
@@ -395,6 +420,7 @@ class ClaimService extends BaseService {
.emit(ClaimService.Events.FULFILLMENT_CREATED, { .emit(ClaimService.Events.FULFILLMENT_CREATED, {
id: id, id: id,
fulfillment_id: fulfillment.id, fulfillment_id: fulfillment.id,
no_notification: claim.no_notification,
}) })
} }
@@ -430,21 +456,38 @@ class ClaimService extends BaseService {
.withTransaction(manager) .withTransaction(manager)
.emit(ClaimService.Events.REFUND_PROCESSED, { .emit(ClaimService.Events.REFUND_PROCESSED, {
id, id,
no_notification: result.no_notification,
}) })
return result return result
}) })
} }
async createShipment(id, fulfillmentId, trackingLinks, metadata = []) { async createShipment(
id,
fulfillmentId,
trackingLinks,
config = {
metadata: {},
no_notification: undefined,
}
) {
const { metadata, no_notification } = config
return this.atomicPhase_(async manager => { return this.atomicPhase_(async manager => {
const claim = await this.retrieve(id, { const claim = await this.retrieve(id, {
relations: ["additional_items"], relations: ["additional_items"],
}) })
const evaluatedNoNotification =
no_notification !== undefined ? no_notification : claim.no_notification
const shipment = await this.fulfillmentService_ const shipment = await this.fulfillmentService_
.withTransaction(manager) .withTransaction(manager)
.createShipment(fulfillmentId, trackingLinks, metadata) .createShipment(fulfillmentId, trackingLinks, {
metadata,
no_notification: evaluatedNoNotification,
})
claim.fulfillment_status = "shipped" claim.fulfillment_status = "shipped"
@@ -474,6 +517,7 @@ class ClaimService extends BaseService {
.emit(ClaimService.Events.SHIPMENT_CREATED, { .emit(ClaimService.Events.SHIPMENT_CREATED, {
id, id,
fulfillment_id: shipment.id, fulfillment_id: shipment.id,
no_notification: evaluatedNoNotification,
}) })
return result return result
@@ -524,6 +568,7 @@ class ClaimService extends BaseService {
.withTransaction(manager) .withTransaction(manager)
.emit(ClaimService.Events.CANCELED, { .emit(ClaimService.Events.CANCELED, {
id: result.id, id: result.id,
no_notification: result.no_notification,
}) })
return result return result
+50 -2
View File
@@ -10,6 +10,7 @@ import { Brackets } from "typeorm"
class DraftOrderService extends BaseService { class DraftOrderService extends BaseService {
static Events = { static Events = {
CREATED: "draft_order.created", CREATED: "draft_order.created",
UPDATED: "draft_order.updated",
} }
constructor({ constructor({
@@ -248,7 +249,13 @@ class DraftOrderService extends BaseService {
) )
} }
const { items, shipping_methods, discounts, ...rest } = data const {
items,
shipping_methods,
discounts,
no_notification_order,
...rest
} = data
if (discounts) { if (discounts) {
for (const { code } of discounts) { for (const { code } of discounts) {
@@ -263,7 +270,10 @@ class DraftOrderService extends BaseService {
.withTransaction(manager) .withTransaction(manager)
.create({ type: "draft_order", ...rest }) .create({ type: "draft_order", ...rest })
const draftOrder = draftOrderRepo.create({ cart_id: createdCart.id }) const draftOrder = draftOrderRepo.create({
cart_id: createdCart.id,
no_notification_order,
})
const result = await draftOrderRepo.save(draftOrder) const result = await draftOrderRepo.save(draftOrder)
await this.eventBus_ await this.eventBus_
@@ -335,6 +345,44 @@ class DraftOrderService extends BaseService {
await draftOrderRepo.save(draftOrder) await draftOrderRepo.save(draftOrder)
}) })
} }
/**
* Updates a draft order with the given data
* @param {String} doId - id of the draft order
* @param {DraftOrder} data - values to update the order with
* @returns {Promise<DraftOrder>} the updated draft order
*/
async update(doId, data) {
return this.atomicPhase_(async manager => {
const doRepo = manager.getCustomRepository(this.draftOrderRepository_)
const draftOrder = await this.retrieve(doId)
let touched = false
if (draftOrder.status === "completed") {
throw new MedusaError(
MedusaError.Types.NOT_ALLOWED,
"Can't update a draft order which is complete"
)
}
if (data.no_notification_order !== undefined) {
touched = true
draftOrder.no_notification_order = data.no_notification_order
}
if (touched) {
doRepo.save(draftOrder)
await this.eventBus_
.withTransaction(manager)
.emit(DraftOrderService.Events.UPDATED, {
id: draftOrder.id,
})
}
return draftOrder
})
}
} }
export default DraftOrderService export default DraftOrderService
+15 -2
View File
@@ -241,10 +241,19 @@ class FulfillmentService extends BaseService {
* tracking numbers and potentially more metadata. * tracking numbers and potentially more metadata.
* @param {Order} fulfillmentId - the fulfillment to ship * @param {Order} fulfillmentId - the fulfillment to ship
* @param {TrackingLink[]} trackingNumbers - tracking numbers for the shipment * @param {TrackingLink[]} trackingNumbers - tracking numbers for the shipment
* @param {object} metadata - potential metadata to add * @param {object} config - potential configuration settings, such as no_notification and metadata
* @return {Fulfillment} the shipped fulfillment * @return {Fulfillment} the shipped fulfillment
*/ */
async createShipment(fulfillmentId, trackingLinks, metadata) { async createShipment(
fulfillmentId,
trackingLinks,
config = {
metadata: {},
no_notification: undefined,
}
) {
const { metadata, no_notification } = config
return this.atomicPhase_(async manager => { return this.atomicPhase_(async manager => {
const fulfillmentRepository = manager.getCustomRepository( const fulfillmentRepository = manager.getCustomRepository(
this.fulfillmentRepository_ this.fulfillmentRepository_
@@ -264,6 +273,10 @@ class FulfillmentService extends BaseService {
trackingLinkRepo.create(tl) trackingLinkRepo.create(tl)
) )
if (no_notification) {
fulfillment.no_notification = no_notification
}
fulfillment.metadata = { fulfillment.metadata = {
...fulfillment.metadata, ...fulfillment.metadata,
...metadata, ...metadata,
@@ -164,6 +164,9 @@ class NotificationService extends BaseService {
if (!subs) { if (!subs) {
return Promise.resolve() return Promise.resolve()
} }
if (data["no_notification"] === true) {
return
}
return Promise.all( return Promise.all(
subs.map(async providerId => { subs.map(async providerId => {
+68 -5
View File
@@ -333,7 +333,6 @@ class OrderService extends BaseService {
const rels = query.relations const rels = query.relations
delete query.relations delete query.relations
const raw = await orderRepo.findOneWithRelations(rels, query) const raw = await orderRepo.findOneWithRelations(rels, query)
if (!raw) { if (!raw) {
throw new MedusaError( throw new MedusaError(
MedusaError.Types.NOT_FOUND, MedusaError.Types.NOT_FOUND,
@@ -408,6 +407,7 @@ class OrderService extends BaseService {
OrderService.Events.COMPLETED, OrderService.Events.COMPLETED,
{ {
id: orderId, id: orderId,
no_notification: order.no_notification,
} }
) )
@@ -507,6 +507,7 @@ class OrderService extends BaseService {
.retrieveByCartId(cart.id) .retrieveByCartId(cart.id)
toCreate.draft_order_id = draft.id toCreate.draft_order_id = draft.id
toCreate.no_notification = draft.no_notification_order
} }
const o = await orderRepo.create(toCreate) const o = await orderRepo.create(toCreate)
@@ -553,6 +554,7 @@ class OrderService extends BaseService {
.withTransaction(manager) .withTransaction(manager)
.emit(OrderService.Events.PLACED, { .emit(OrderService.Events.PLACED, {
id: result.id, id: result.id,
no_notification: result.no_notification,
}) })
return result return result
@@ -571,7 +573,17 @@ class OrderService extends BaseService {
* the fulfillment * the fulfillment
* @return {order} the resulting order following the update. * @return {order} the resulting order following the update.
*/ */
async createShipment(orderId, fulfillmentId, trackingLinks, metadata = {}) { async createShipment(
orderId,
fulfillmentId,
trackingLinks,
config = {
metadata: {},
no_notification: undefined,
}
) {
const { metadata, no_notification } = config
return this.atomicPhase_(async manager => { return this.atomicPhase_(async manager => {
const order = await this.retrieve(orderId, { relations: ["items"] }) const order = await this.retrieve(orderId, { relations: ["items"] })
const shipment = await this.fulfillmentService_.retrieve(fulfillmentId) const shipment = await this.fulfillmentService_.retrieve(fulfillmentId)
@@ -583,9 +595,17 @@ class OrderService extends BaseService {
) )
} }
const evaluatedNoNotification =
no_notification !== undefined
? no_notification
: shipment.no_notification
const shipmentRes = await this.fulfillmentService_ const shipmentRes = await this.fulfillmentService_
.withTransaction(manager) .withTransaction(manager)
.createShipment(fulfillmentId, trackingLinks, metadata) .createShipment(fulfillmentId, trackingLinks, {
metadata,
no_notification: evaluatedNoNotification,
})
order.fulfillment_status = "shipped" order.fulfillment_status = "shipped"
for (const item of order.items) { for (const item of order.items) {
@@ -614,6 +634,7 @@ class OrderService extends BaseService {
.emit(OrderService.Events.SHIPMENT_CREATED, { .emit(OrderService.Events.SHIPMENT_CREATED, {
id: orderId, id: orderId,
fulfillment_id: shipmentRes.id, fulfillment_id: shipmentRes.id,
no_notification: evaluatedNoNotification,
}) })
return result return result
@@ -634,6 +655,7 @@ class OrderService extends BaseService {
.withTransaction(manager) .withTransaction(manager)
.emit(OrderService.Events.PLACED, { .emit(OrderService.Events.PLACED, {
id: result.id, id: result.id,
no_notification: order.no_notification,
}) })
return result return result
}) })
@@ -801,6 +823,10 @@ class OrderService extends BaseService {
await this.updateBillingAddress_(order, update.billing_address) await this.updateBillingAddress_(order, update.billing_address)
} }
if ("no_notification" in update) {
order.no_notification = update.no_notification
}
if ("items" in update) { if ("items" in update) {
for (const item of update.items) { for (const item of update.items) {
await this.lineItemService_.withTransaction(manager).create({ await this.lineItemService_.withTransaction(manager).create({
@@ -821,6 +847,7 @@ class OrderService extends BaseService {
.withTransaction(manager) .withTransaction(manager)
.emit(OrderService.Events.UPDATED, { .emit(OrderService.Events.UPDATED, {
id: orderId, id: orderId,
no_notification: order.no_notification,
}) })
return result return result
}) })
@@ -871,6 +898,7 @@ class OrderService extends BaseService {
.withTransaction(manager) .withTransaction(manager)
.emit(OrderService.Events.CANCELED, { .emit(OrderService.Events.CANCELED, {
id: order.id, id: order.id,
no_notification: order.no_notification,
}) })
return result return result
}) })
@@ -899,6 +927,7 @@ class OrderService extends BaseService {
id: orderId, id: orderId,
payment_id: p.id, payment_id: p.id,
error: err, error: err,
no_notification: order.no_notification,
}) })
}) })
@@ -924,6 +953,7 @@ class OrderService extends BaseService {
.withTransaction(manager) .withTransaction(manager)
.emit(OrderService.Events.PAYMENT_CAPTURED, { .emit(OrderService.Events.PAYMENT_CAPTURED, {
id: result.id, id: result.id,
no_notification: order.no_notification,
}) })
} }
@@ -970,7 +1000,16 @@ class OrderService extends BaseService {
* @param {string} orderId - id of order to cancel. * @param {string} orderId - id of order to cancel.
* @return {Promise} result of the update operation. * @return {Promise} result of the update operation.
*/ */
async createFulfillment(orderId, itemsToFulfill, metadata = {}) { async createFulfillment(
orderId,
itemsToFulfill,
config = {
no_notification: undefined,
metadata: {},
}
) {
const { metadata, no_notification } = config
return this.atomicPhase_(async manager => { return this.atomicPhase_(async manager => {
const order = await this.retrieve(orderId, { const order = await this.retrieve(orderId, {
select: [ select: [
@@ -979,6 +1018,8 @@ class OrderService extends BaseService {
"discount_total", "discount_total",
"tax_total", "tax_total",
"gift_card_total", "gift_card_total",
"no_notification",
"id",
"total", "total",
], ],
relations: [ relations: [
@@ -1007,6 +1048,7 @@ class OrderService extends BaseService {
.withTransaction(manager) .withTransaction(manager)
.createFulfillment(order, itemsToFulfill, { .createFulfillment(order, itemsToFulfill, {
metadata, metadata,
no_notification: no_notification,
order_id: orderId, order_id: orderId,
}) })
let successfullyFulfilled = [] let successfullyFulfilled = []
@@ -1046,12 +1088,16 @@ class OrderService extends BaseService {
order.fulfillments = [...order.fulfillments, ...fulfillments] order.fulfillments = [...order.fulfillments, ...fulfillments]
const result = await orderRepo.save(order) const result = await orderRepo.save(order)
const evaluatedNoNotification =
no_notification !== undefined ? no_notification : order.no_notification
for (const fulfillment of fulfillments) { for (const fulfillment of fulfillments) {
await this.eventBus_ await this.eventBus_
.withTransaction(manager) .withTransaction(manager)
.emit(OrderService.Events.FULFILLMENT_CREATED, { .emit(OrderService.Events.FULFILLMENT_CREATED, {
id: orderId, id: orderId,
fulfillment_id: fulfillment.id, fulfillment_id: fulfillment.id,
no_notification: evaluatedNoNotification,
}) })
} }
@@ -1107,7 +1153,17 @@ class OrderService extends BaseService {
/** /**
* Refunds a given amount back to the customer. * Refunds a given amount back to the customer.
*/ */
async createRefund(orderId, refundAmount, reason, note) { async createRefund(
orderId,
refundAmount,
reason,
note,
config = {
no_notification: undefined,
}
) {
const { no_notification } = config
return this.atomicPhase_(async manager => { return this.atomicPhase_(async manager => {
const order = await this.retrieve(orderId, { const order = await this.retrieve(orderId, {
select: ["refundable_amount", "total", "refunded_total"], select: ["refundable_amount", "total", "refunded_total"],
@@ -1126,9 +1182,14 @@ class OrderService extends BaseService {
.refundPayment(order.payments, refundAmount, reason, note) .refundPayment(order.payments, refundAmount, reason, note)
const result = await this.retrieve(orderId) const result = await this.retrieve(orderId)
const evaluatedNoNotification =
no_notification !== undefined ? no_notification : order.no_notification
this.eventBus_.emit(OrderService.Events.REFUND_CREATED, { this.eventBus_.emit(OrderService.Events.REFUND_CREATED, {
id: result.id, id: result.id,
refund_id: refund.id, refund_id: refund.id,
no_notification: evaluatedNoNotification,
}) })
return result return result
}) })
@@ -1232,6 +1293,7 @@ class OrderService extends BaseService {
.emit(OrderService.Events.RETURN_ACTION_REQUIRED, { .emit(OrderService.Events.RETURN_ACTION_REQUIRED, {
id: result.id, id: result.id,
return_id: receivedReturn.id, return_id: receivedReturn.id,
no_notification: receivedReturn.no_notification,
}) })
return result return result
} }
@@ -1263,6 +1325,7 @@ class OrderService extends BaseService {
.emit(OrderService.Events.ITEMS_RETURNED, { .emit(OrderService.Events.ITEMS_RETURNED, {
id: order.id, id: order.id,
return_id: receivedReturn.id, return_id: receivedReturn.id,
no_notification: receivedReturn.no_notification,
}) })
return result return result
}) })
+1
View File
@@ -330,6 +330,7 @@ class ReturnService extends BaseService {
reason_id: i.reason_id, reason_id: i.reason_id,
note: i.note, note: i.note,
metadata: i.metadata, metadata: i.metadata,
no_notification: data.no_notification,
}) })
) )
+95 -12
View File
@@ -16,6 +16,7 @@ class SwapService extends BaseService {
PAYMENT_CAPTURE_FAILED: "swap.payment_capture_failed", PAYMENT_CAPTURE_FAILED: "swap.payment_capture_failed",
PROCESS_REFUND_FAILED: "swap.process_refund_failed", PROCESS_REFUND_FAILED: "swap.process_refund_failed",
REFUND_PROCESSED: "swap.refund_processed", REFUND_PROCESSED: "swap.refund_processed",
FULFILLMENT_CREATED: "swap.fulfillment_created",
} }
constructor({ constructor({
@@ -210,6 +211,9 @@ class SwapService extends BaseService {
* the customer. * the customer.
* @param {ReturnShipping?} returnShipping - an optional shipping method for * @param {ReturnShipping?} returnShipping - an optional shipping method for
* returning the returnItems. * returning the returnItems.
* @param {Object} custom - contains relevant custom information. This object may
* include no_notification which will disable sending notification when creating
* swap. If set, it overrules the attribute inherited from the order.
* @returns {Promise<Swap>} the newly created swap. * @returns {Promise<Swap>} the newly created swap.
*/ */
async create( async create(
@@ -217,8 +221,11 @@ class SwapService extends BaseService {
returnItems, returnItems,
additionalItems, additionalItems,
returnShipping, returnShipping,
custom = {} custom = {
no_notification: undefined,
}
) { ) {
const { no_notification, ...rest } = custom
return this.atomicPhase_(async manager => { return this.atomicPhase_(async manager => {
if ( if (
order.fulfillment_status === "not_fulfilled" || order.fulfillment_status === "not_fulfilled" ||
@@ -240,13 +247,17 @@ class SwapService extends BaseService {
}) })
) )
const evaluatedNoNotification =
no_notification !== undefined ? no_notification : order.no_notification
const swapRepo = manager.getCustomRepository(this.swapRepository_) const swapRepo = manager.getCustomRepository(this.swapRepository_)
const created = swapRepo.create({ const created = swapRepo.create({
...custom, ...rest,
fulfillment_status: "not_fulfilled", fulfillment_status: "not_fulfilled",
payment_status: "not_paid", payment_status: "not_paid",
order_id: order.id, order_id: order.id,
additional_items: newItems, additional_items: newItems,
no_notification: evaluatedNoNotification,
}) })
const result = await swapRepo.save(created) const result = await swapRepo.save(created)
@@ -256,12 +267,14 @@ class SwapService extends BaseService {
order_id: order.id, order_id: order.id,
items: returnItems, items: returnItems,
shipping_method: returnShipping, shipping_method: returnShipping,
no_notification: evaluatedNoNotification,
}) })
await this.eventBus_ await this.eventBus_
.withTransaction(manager) .withTransaction(manager)
.emit(SwapService.Events.CREATED, { .emit(SwapService.Events.CREATED, {
id: result.id, id: result.id,
no_notification: evaluatedNoNotification,
}) })
return result return result
@@ -298,9 +311,14 @@ class SwapService extends BaseService {
} catch (err) { } catch (err) {
swap.payment_status = "requires_action" swap.payment_status = "requires_action"
const result = await swapRepo.save(swap) const result = await swapRepo.save(swap)
await this.eventBus_ await this.eventBus_
.withTransaction(manager) .withTransaction(manager)
.emit(SwapService.Events.PROCESS_REFUND_FAILED, result) .emit(SwapService.Events.PROCESS_REFUND_FAILED, {
id: result.id,
no_notification: swap.no_notification,
})
return result return result
} }
@@ -310,7 +328,11 @@ class SwapService extends BaseService {
await this.eventBus_ await this.eventBus_
.withTransaction(manager) .withTransaction(manager)
.emit(SwapService.Events.REFUND_PROCESSED, result) .emit(SwapService.Events.REFUND_PROCESSED, {
id: result.id,
no_notification: swap.no_notification,
})
return result return result
} else if (swap.difference_due === 0) { } else if (swap.difference_due === 0) {
if (swap.payment_status === "difference_refunded") { if (swap.payment_status === "difference_refunded") {
@@ -320,9 +342,14 @@ class SwapService extends BaseService {
swap.payment_status = "difference_refunded" swap.payment_status = "difference_refunded"
const result = await swapRepo.save(swap) const result = await swapRepo.save(swap)
await this.eventBus_ await this.eventBus_
.withTransaction(manager) .withTransaction(manager)
.emit(SwapService.Events.REFUND_PROCESSED, result) .emit(SwapService.Events.REFUND_PROCESSED, {
id: result.id,
no_notification: swap.no_notification,
})
return result return result
} }
@@ -337,18 +364,28 @@ class SwapService extends BaseService {
} catch (err) { } catch (err) {
swap.payment_status = "requires_action" swap.payment_status = "requires_action"
const result = await swapRepo.save(swap) const result = await swapRepo.save(swap)
await this.eventBus_ await this.eventBus_
.withTransaction(manager) .withTransaction(manager)
.emit(SwapService.Events.PAYMENT_CAPTURE_FAILED, result) .emit(SwapService.Events.PAYMENT_CAPTURE_FAILED, {
id: swap.id,
no_notification: swap.no_notification,
})
return result return result
} }
swap.payment_status = "captured" swap.payment_status = "captured"
const result = await swapRepo.save(swap) const result = await swapRepo.save(swap)
await this.eventBus_ await this.eventBus_
.withTransaction(manager) .withTransaction(manager)
.emit(SwapService.Events.PAYMENT_CAPTURED, result) .emit(SwapService.Events.PAYMENT_CAPTURED, {
id: result.id,
no_notification: swap.no_notification,
})
return result return result
}) })
} }
@@ -361,6 +398,10 @@ class SwapService extends BaseService {
swap.metadata = this.setMetadata_(swap, update.metadata) swap.metadata = this.setMetadata_(swap, update.metadata)
} }
if ("no_notification" in update) {
swap.no_notification = update.no_notification
}
if ("shipping_address" in update) { if ("shipping_address" in update) {
await this.updateShippingAddress_(swap, update.shipping_address) await this.updateShippingAddress_(swap, update.shipping_address)
} }
@@ -562,6 +603,7 @@ class SwapService extends BaseService {
.withTransaction(manager) .withTransaction(manager)
.emit(SwapService.Events.PAYMENT_COMPLETED, { .emit(SwapService.Events.PAYMENT_COMPLETED, {
id: swap.id, id: swap.id,
no_notification: swap.no_notification,
}) })
return result return result
@@ -609,10 +651,18 @@ class SwapService extends BaseService {
* Fulfills the addtional items associated with the swap. Will call the * Fulfills the addtional items associated with the swap. Will call the
* fulfillment providers associated with the shipping methods. * fulfillment providers associated with the shipping methods.
* @param {string} swapId - the id of the swap to fulfill, * @param {string} swapId - the id of the swap to fulfill,
* @param {object} metadata - optional metadata to attach to the fulfillment. * @param {object} config - optional configurations, includes optional metadata to attach to the shipment, and a no_notification flag.
* @returns {Promise<Swap>} the updated swap with new status and fulfillments. * @returns {Promise<Swap>} the updated swap with new status and fulfillments.
*/ */
async createFulfillment(swapId, metadata = {}) { async createFulfillment(
swapId,
config = {
metadata: {},
no_notification: undefined,
}
) {
const { metadata, no_notification } = config
return this.atomicPhase_(async manager => { return this.atomicPhase_(async manager => {
const swap = await this.retrieve(swapId, { const swap = await this.retrieve(swapId, {
relations: [ relations: [
@@ -642,6 +692,9 @@ class SwapService extends BaseService {
) )
} }
const evaluatedNoNotification =
no_notification !== undefined ? no_notification : swap.no_notification
swap.fulfillments = await this.fulfillmentService_ swap.fulfillments = await this.fulfillmentService_
.withTransaction(manager) .withTransaction(manager)
.createFulfillment( .createFulfillment(
@@ -658,6 +711,7 @@ class SwapService extends BaseService {
items: swap.additional_items, items: swap.additional_items,
shipping_methods: swap.shipping_methods, shipping_methods: swap.shipping_methods,
is_swap: true, is_swap: true,
no_notification: evaluatedNoNotification,
}, },
swap.additional_items.map(i => ({ swap.additional_items.map(i => ({
item_id: i.id, item_id: i.id,
@@ -700,6 +754,17 @@ class SwapService extends BaseService {
const swapRepo = manager.getCustomRepository(this.swapRepository_) const swapRepo = manager.getCustomRepository(this.swapRepository_)
const result = await swapRepo.save(swap) const result = await swapRepo.save(swap)
await this.eventBus_.withTransaction(manager).emit(
SwapService.Events.FULFILLMENT_CREATED,
{
id: swapId,
fulfillment_id: result.id,
no_notification: evaluatedNoNotification,
}
)
return result return result
}) })
} }
@@ -711,19 +776,35 @@ class SwapService extends BaseService {
* has been shipped * has been shipped
* @param {TrackingLink[]} trackingLinks - the tracking numbers associated * @param {TrackingLink[]} trackingLinks - the tracking numbers associated
* with the shipment * with the shipment
* @param {object} metadata - optional metadata to attach to the shipment. * @param {object} config - optional configurations, includes optional metadata to attach to the shipment, and a noNotification flag.
* @returns {Promise<Swap>} the updated swap with new fulfillments and status. * @returns {Promise<Swap>} the updated swap with new fulfillments and status.
*/ */
async createShipment(swapId, fulfillmentId, trackingLinks, metadata = {}) { async createShipment(
swapId,
fulfillmentId,
trackingLinks,
config = {
metadata: {},
no_notification: undefined,
}
) {
const { metadata, no_notification } = config
return this.atomicPhase_(async manager => { return this.atomicPhase_(async manager => {
const swap = await this.retrieve(swapId, { const swap = await this.retrieve(swapId, {
relations: ["additional_items"], relations: ["additional_items"],
}) })
const evaluatedNoNotification =
no_notification !== undefined ? no_notification : swap.no_notification
// Update the fulfillment to register // Update the fulfillment to register
const shipment = await this.fulfillmentService_ const shipment = await this.fulfillmentService_
.withTransaction(manager) .withTransaction(manager)
.createShipment(fulfillmentId, trackingLinks, metadata) .createShipment(fulfillmentId, trackingLinks, {
metadata,
no_notification: evaluatedNoNotification,
})
swap.fulfillment_status = "shipped" swap.fulfillment_status = "shipped"
@@ -753,6 +834,7 @@ class SwapService extends BaseService {
.emit(SwapService.Events.SHIPMENT_CREATED, { .emit(SwapService.Events.SHIPMENT_CREATED, {
id: swapId, id: swapId,
fulfillment_id: shipment.id, fulfillment_id: shipment.id,
no_notification: swap.no_notification,
}) })
return result return result
}) })
@@ -809,6 +891,7 @@ class SwapService extends BaseService {
.emit(SwapService.Events.RECEIVED, { .emit(SwapService.Events.RECEIVED, {
id: id, id: id,
order_id: result.order_id, order_id: result.order_id,
no_notification: swap.no_notification,
}) })
return result return result