added changes to swap and notification service

This commit is contained in:
--list committed 2021-06-09 11:19:44 +02:00
1 parent 4f76088382
commit 1f91c0ec7c
10 files changed
+2397 -2297

No files matched your search

+839 -833
View File
File diff suppressed because it is too large. Load diff
+1125 -1118
View File
File diff suppressed because it is too large. Load diff
+286 -275
View File
@@ -976,6 +976,55 @@
} }
} }
}, },
"/gift-cards/{code}": {
"get": {
"operationId": "GetGiftCardsCode",
"summary": "Retrieve Gift Card by Code",
"description": "Retrieves a Gift Card by its associated unqiue code.",
"parameters": [
{
"in": "path",
"name": "code",
"required": true,
"description": "The unique Gift Card code.",
"schema": {
"type": "string"
}
}
],
"tags": [
"Gift Card"
],
"responses": {
"200": {
"description": "OK",
"content": {
"application/json": {
"schema": {
"properties": {
"id": {
"description": "The id of the Gift Card"
},
"code": {
"description": "The code of the Gift Card"
},
"value": {
"description": "The original value of the Gift Card."
},
"balance": {
"description": "The current balanace of the Gift Card"
},
"region": {
"$ref": "#/components/schemas/region"
}
}
}
}
}
}
}
}
},
"/customers/{id}/addresses": { "/customers/{id}/addresses": {
"post": { "post": {
"operationId": "PostCustomersCustomerAddresses", "operationId": "PostCustomersCustomerAddresses",
@@ -1481,277 +1530,6 @@
} }
} }
}, },
"/gift-cards/{code}": {
"get": {
"operationId": "GetGiftCardsCode",
"summary": "Retrieve Gift Card by Code",
"description": "Retrieves a Gift Card by its associated unqiue code.",
"parameters": [
{
"in": "path",
"name": "code",
"required": true,
"description": "The unique Gift Card code.",
"schema": {
"type": "string"
}
}
],
"tags": [
"Gift Card"
],
"responses": {
"200": {
"description": "OK",
"content": {
"application/json": {
"schema": {
"properties": {
"id": {
"description": "The id of the Gift Card"
},
"code": {
"description": "The code of the Gift Card"
},
"value": {
"description": "The original value of the Gift Card."
},
"balance": {
"description": "The current balanace of the Gift Card"
},
"region": {
"$ref": "#/components/schemas/region"
}
}
}
}
}
}
}
}
},
"/products/{id}": {
"get": {
"operationId": "GetProductsProduct",
"summary": "Retrieves a Product",
"description": "Retrieves a Product.",
"parameters": [
{
"in": "path",
"name": "id",
"required": true,
"description": "The id of the Product.",
"schema": {
"type": "string"
}
}
],
"tags": [
"Product"
],
"responses": {
"200": {
"description": "OK",
"content": {
"application/json": {
"schema": {
"properties": {
"product": {
"$ref": "#/components/schemas/product"
}
}
}
}
}
}
}
}
},
"/products": {
"get": {
"operationId": "GetProducts",
"summary": "List Products",
"description": "Retrieves a list of Products.",
"tags": [
"Product"
],
"responses": {
"200": {
"description": "OK",
"content": {
"application/json": {
"schema": {
"properties": {
"count": {
"description": "The total number of Products.",
"type": "integer"
},
"offset": {
"description": "The offset for pagination.",
"type": "integer"
},
"limit": {
"description": "The maxmimum number of Products to return,",
"type": "integer"
},
"products": {
"type": "array",
"items": {
"$ref": "#/components/schemas/product"
}
}
}
}
}
}
}
}
}
},
"/return-reasons/{id}": {
"get": {
"operationId": "GetReturnReasonsReason",
"summary": "Retrieve a Return Reason",
"description": "Retrieves a Return Reason.",
"parameters": [
{
"in": "path",
"name": "id",
"required": true,
"description": "The id of the Return Reason.",
"schema": {
"type": "string"
}
}
],
"tags": [
"Return Reason"
],
"responses": {
"200": {
"description": "OK",
"content": {
"application/json": {
"schema": {
"properties": {
"return_reason": {
"$ref": "#/components/schemas/return_reason"
}
}
}
}
}
}
}
}
},
"/return-reasons": {
"get": {
"operationId": "GetReturnReasons",
"summary": "List Return Reasons",
"description": "Retrieves a list of Return Reasons.",
"tags": [
"Return Reason"
],
"responses": {
"200": {
"description": "OK",
"content": {
"application/json": {
"schema": {
"properties": {
"return_reasons": {
"type": "array",
"items": {
"$ref": "#/components/schemas/return_reason"
}
}
}
}
}
}
}
}
}
},
"/regions/{id}": {
"get": {
"operationId": "GetRegionsRegion",
"summary": "Retrieves a Region",
"description": "Retrieves a Region.",
"parameters": [
{
"in": "path",
"name": "id",
"required": true,
"description": "The id of the Region.",
"schema": {
"type": "string"
}
}
],
"tags": [
"Region"
],
"responses": {
"200": {
"description": "OK",
"content": {
"application/json": {
"schema": {
"properties": {
"region": {
"$ref": "#/components/schemas/region"
}
}
}
}
}
}
}
}
},
"/regions": {
"get": {
"operationId": "GetRegions",
"summary": "List Regions",
"description": "Retrieves a list of Regions.",
"tags": [
"Region"
],
"responses": {
"200": {
"description": "OK",
"content": {
"application/json": {
"schema": {
"properties": {
"count": {
"description": "The total number of regions.",
"type": "integer"
},
"offset": {
"description": "The offset for pagination.",
"type": "integer"
},
"limit": {
"description": "The maxmimum number of regions to return,",
"type": "integer"
},
"regions": {
"type": "array",
"items": {
"$ref": "#/components/schemas/region"
}
}
}
}
}
}
}
}
}
},
"/orders/cart/{cart_id}": { "/orders/cart/{cart_id}": {
"get": { "get": {
"operationId": "GetOrdersOrderCartId", "operationId": "GetOrdersOrderCartId",
@@ -1872,6 +1650,162 @@
} }
} }
}, },
"/products/{id}": {
"get": {
"operationId": "GetProductsProduct",
"summary": "Retrieves a Product",
"description": "Retrieves a Product.",
"parameters": [
{
"in": "path",
"name": "id",
"required": true,
"description": "The id of the Product.",
"schema": {
"type": "string"
}
}
],
"tags": [
"Product"
],
"responses": {
"200": {
"description": "OK",
"content": {
"application/json": {
"schema": {
"properties": {
"product": {
"$ref": "#/components/schemas/product"
}
}
}
}
}
}
}
}
},
"/products": {
"get": {
"operationId": "GetProducts",
"summary": "List Products",
"description": "Retrieves a list of Products.",
"tags": [
"Product"
],
"responses": {
"200": {
"description": "OK",
"content": {
"application/json": {
"schema": {
"properties": {
"count": {
"description": "The total number of Products.",
"type": "integer"
},
"offset": {
"description": "The offset for pagination.",
"type": "integer"
},
"limit": {
"description": "The maxmimum number of Products to return,",
"type": "integer"
},
"products": {
"type": "array",
"items": {
"$ref": "#/components/schemas/product"
}
}
}
}
}
}
}
}
}
},
"/regions/{id}": {
"get": {
"operationId": "GetRegionsRegion",
"summary": "Retrieves a Region",
"description": "Retrieves a Region.",
"parameters": [
{
"in": "path",
"name": "id",
"required": true,
"description": "The id of the Region.",
"schema": {
"type": "string"
}
}
],
"tags": [
"Region"
],
"responses": {
"200": {
"description": "OK",
"content": {
"application/json": {
"schema": {
"properties": {
"region": {
"$ref": "#/components/schemas/region"
}
}
}
}
}
}
}
}
},
"/regions": {
"get": {
"operationId": "GetRegions",
"summary": "List Regions",
"description": "Retrieves a list of Regions.",
"tags": [
"Region"
],
"responses": {
"200": {
"description": "OK",
"content": {
"application/json": {
"schema": {
"properties": {
"count": {
"description": "The total number of regions.",
"type": "integer"
},
"offset": {
"description": "The offset for pagination.",
"type": "integer"
},
"limit": {
"description": "The maxmimum number of regions to return,",
"type": "integer"
},
"regions": {
"type": "array",
"items": {
"$ref": "#/components/schemas/region"
}
}
}
}
}
}
}
}
}
},
"/returns": { "/returns": {
"post": { "post": {
"operationId": "PostReturns", "operationId": "PostReturns",
@@ -1911,6 +1845,10 @@
"description": "The id of the Shipping Option to create the Shipping Method from." "description": "The id of the Shipping Option to create the Shipping Method from."
} }
} }
},
"no_notification": {
"description": "If set to true no notification will be send",
"type": "boolean"
} }
} }
} }
@@ -1938,6 +1876,72 @@
} }
} }
}, },
"/return-reasons/{id}": {
"get": {
"operationId": "GetReturnReasonsReason",
"summary": "Retrieve a Return Reason",
"description": "Retrieves a Return Reason.",
"parameters": [
{
"in": "path",
"name": "id",
"required": true,
"description": "The id of the Return Reason.",
"schema": {
"type": "string"
}
}
],
"tags": [
"Return Reason"
],
"responses": {
"200": {
"description": "OK",
"content": {
"application/json": {
"schema": {
"properties": {
"return_reason": {
"$ref": "#/components/schemas/return_reason"
}
}
}
}
}
}
}
}
},
"/return-reasons": {
"get": {
"operationId": "GetReturnReasons",
"summary": "List Return Reasons",
"description": "Retrieves a list of Return Reasons.",
"tags": [
"Return Reason"
],
"responses": {
"200": {
"description": "OK",
"content": {
"application/json": {
"schema": {
"properties": {
"return_reasons": {
"type": "array",
"items": {
"$ref": "#/components/schemas/return_reason"
}
}
}
}
}
}
}
}
}
},
"/shipping-options": { "/shipping-options": {
"get": { "get": {
"operationId": "GetShippingOptions", "operationId": "GetShippingOptions",
@@ -2696,10 +2700,6 @@
"$ref": "#/components/schemas/product" "$ref": "#/components/schemas/product"
} }
}, },
"usage_limit": {
"description": "The maximum number of times that a discount can be used.",
"type": "integer"
},
"created_at": { "created_at": {
"description": "The date with timezone at which the resource was created.", "description": "The date with timezone at which the resource was created.",
"type": "string", "type": "string",
@@ -2771,6 +2771,14 @@
"$ref": "#/components/schemas/region" "$ref": "#/components/schemas/region"
} }
}, },
"usage_limit": {
"description": "The maximum number of times that a discount can be used.",
"type": "integer"
},
"usage_count": {
"description": "The number of times a discount has been used.",
"type": "integer"
},
"created_at": { "created_at": {
"description": "The date with timezone at which the resource was created.", "description": "The date with timezone at which the resource was created.",
"type": "string", "type": "string",
@@ -3545,6 +3553,9 @@
}, },
"gift_card_total": { "gift_card_total": {
"type": "integer" "type": "integer"
},
"paid_total": {
"type": "integer"
} }
} }
}, },
+75 -67
View File
@@ -1147,47 +1147,6 @@ paths:
type: array type: array
items: items:
$ref: '#/components/schemas/product' $ref: '#/components/schemas/product'
'/return-reasons/{id}':
get:
operationId: GetReturnReasonsReason
summary: Retrieve a Return Reason
description: Retrieves a Return Reason.
parameters:
- in: path
name: id
required: true
description: The id of the Return Reason.
schema:
type: string
tags:
- Return Reason
responses:
'200':
description: OK
content:
application/json:
schema:
properties:
return_reason:
$ref: '#/components/schemas/return_reason'
/return-reasons:
get:
operationId: GetReturnReasons
summary: List Return Reasons
description: Retrieves a list of Return Reasons.
tags:
- Return Reason
responses:
'200':
description: OK
content:
application/json:
schema:
properties:
return_reasons:
type: array
items:
$ref: '#/components/schemas/return_reason'
'/regions/{id}': '/regions/{id}':
get: get:
operationId: GetRegionsRegion operationId: GetRegionsRegion
@@ -1238,6 +1197,47 @@ paths:
type: array type: array
items: items:
$ref: '#/components/schemas/region' $ref: '#/components/schemas/region'
'/return-reasons/{id}':
get:
operationId: GetReturnReasonsReason
summary: Retrieve a Return Reason
description: Retrieves a Return Reason.
parameters:
- in: path
name: id
required: true
description: The id of the Return Reason.
schema:
type: string
tags:
- Return Reason
responses:
'200':
description: OK
content:
application/json:
schema:
properties:
return_reason:
$ref: '#/components/schemas/return_reason'
/return-reasons:
get:
operationId: GetReturnReasons
summary: List Return Reasons
description: Retrieves a list of Return Reasons.
tags:
- Return Reason
responses:
'200':
description: OK
content:
application/json:
schema:
properties:
return_reasons:
type: array
items:
$ref: '#/components/schemas/return_reason'
/returns: /returns:
post: post:
operationId: PostReturns operationId: PostReturns
@@ -1274,6 +1274,9 @@ paths:
description: >- description: >-
The id of the Shipping Option to create the Shipping The id of the Shipping Option to create the Shipping
Method from. Method from.
no_notification:
description: If set to true no notification will be send
type: boolean
tags: tags:
- Return - Return
responses: responses:
@@ -1285,6 +1288,29 @@ paths:
properties: properties:
return: return:
$ref: '#/components/schemas/return' $ref: '#/components/schemas/return'
'/swaps/{cart_id}':
get:
operationId: GetSwapsSwapCartId
summary: Retrieve Swap by Cart id
description: Retrieves a Swap by the id of the Cart used to confirm the Swap.
parameters:
- in: path
name: cart_id
required: true
description: The id of the Cart
schema:
type: string
tags:
- Swap
responses:
'200':
description: OK
content:
application/json:
schema:
properties:
swap:
$ref: '#/components/schemas/swap'
/shipping-options: /shipping-options:
get: get:
operationId: GetShippingOptions operationId: GetShippingOptions
@@ -1346,29 +1372,6 @@ paths:
type: array type: array
items: items:
$ref: '#/components/schemas/shipping_option' $ref: '#/components/schemas/shipping_option'
'/swaps/{cart_id}':
get:
operationId: GetSwapsSwapCartId
summary: Retrieve Swap by Cart id
description: Retrieves a Swap by the id of the Cart used to confirm the Swap.
parameters:
- in: path
name: cart_id
required: true
description: The id of the Cart
schema:
type: string
tags:
- Swap
responses:
'200':
description: OK
content:
application/json:
schema:
properties:
swap:
$ref: '#/components/schemas/swap'
'/variants/{variant_id}': '/variants/{variant_id}':
get: get:
operationId: GetVariantsVariant operationId: GetVariantsVariant
@@ -1829,9 +1832,6 @@ components:
type: array type: array
items: items:
$ref: '#/components/schemas/product' $ref: '#/components/schemas/product'
usage_limit:
description: The maximum number of times that a discount can be used.
type: integer
created_at: created_at:
description: The date with timezone at which the resource was created. description: The date with timezone at which the resource was created.
type: string type: string
@@ -1894,6 +1894,12 @@ components:
type: array type: array
items: items:
$ref: '#/components/schemas/region' $ref: '#/components/schemas/region'
usage_limit:
description: The maximum number of times that a discount can be used.
type: integer
usage_count:
description: The number of times a discount has been used.
type: integer
created_at: created_at:
description: The date with timezone at which the resource was created. description: The date with timezone at which the resource was created.
type: string type: string
@@ -2527,6 +2533,8 @@ components:
type: integer type: integer
gift_card_total: gift_card_total:
type: integer type: integer
paid_total:
type: integer
payment_provider: payment_provider:
title: Payment Provider title: Payment Provider
description: Represents a Payment Provider plugin and holds its installation status. description: Represents a Payment Provider plugin and holds its installation status.
@@ -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.
* 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,6 +138,7 @@ export default async (req, res) => {
value.return_items, value.return_items,
value.additional_items, value.additional_items,
value.return_shipping, value.return_shipping,
no_notification,
{ idempotency_key: idempotencyKey.idempotency_key } { idempotency_key: idempotencyKey.idempotency_key }
) )
@@ -31,6 +31,10 @@ import { MedusaError, Validator } from "medusa-core-utils"
* option_id: * option_id:
* type: string * type: string
* description: The id of the Shipping Option to create the Shipping Method from. * description: The id of the Shipping Option to create the Shipping Method from.
* no_notification:
* description: If set to true no notification will be send
* type: boolean
*
* tags: * tags:
* - Return * - Return
* responses: * responses:
@@ -59,6 +63,7 @@ export default async (req, res) => {
option_id: Validator.string().optional(), option_id: Validator.string().optional(),
}) })
.optional(), .optional(),
no_notification: Validator.boolean().optional()
}) })
const { value, error } = schema.validate(req.body) const { value, error } = schema.validate(req.body)
@@ -132,6 +137,7 @@ export default async (req, res) => {
.emit("order.return_requested", { .emit("order.return_requested", {
id: value.order_id, id: value.order_id,
return_id: createdReturn.id, return_id: createdReturn.id,
no_notification: no_notification
}) })
return { return {
@@ -1,8 +1,7 @@
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 = {
@@ -18,13 +17,15 @@ describe("NotificationService", () => {
}, },
} }
const notificationService = new NotificationService(container)
beforeEach(() => { beforeEach(() => {
jest.clearAllMocks() jest.clearAllMocks()
}) })
describe("send", () =>{
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)
})
})
}) })
@@ -338,6 +338,28 @@ describe("SwapService", () => {
expect(returnService.create).toHaveBeenCalledTimes(1) expect(returnService.create).toHaveBeenCalledTimes(1)
}) })
it.each([
[true, true],
[false, false],
[undefined, undefined]
])( "passes correct 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,
},
input
)
expect(eventBusService.emit).toHaveBeenCalledWith(
expect.anything(),
{"id": undefined, "no_notification": expected})
})
}) })
}) })
@@ -918,4 +940,6 @@ describe("SwapService", () => {
}) })
}) })
}) })
}) })
@@ -164,6 +164,9 @@ class NotificationService extends BaseService {
if (!subs) { if (!subs) {
return return
} }
if(data['no_notification'] === true) {
return
}
return Promise.all( return Promise.all(
subs.map(async providerId => { subs.map(async providerId => {
+3
View File
@@ -204,6 +204,7 @@ 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 {boolean?} noNotification - an optional flag to disable sending notification when creating swap
* @returns {Promise<Swap>} the newly created swap. * @returns {Promise<Swap>} the newly created swap.
*/ */
async create( async create(
@@ -211,6 +212,7 @@ class SwapService extends BaseService {
returnItems, returnItems,
additionalItems, additionalItems,
returnShipping, returnShipping,
noNotification,
custom = {} custom = {}
) { ) {
return this.atomicPhase_(async manager => { return this.atomicPhase_(async manager => {
@@ -256,6 +258,7 @@ class SwapService extends BaseService {
.withTransaction(manager) .withTransaction(manager)
.emit(SwapService.Events.CREATED, { .emit(SwapService.Events.CREATED, {
id: result.id, id: result.id,
no_notification: noNotification,
}) })
return result return result