added changes to swap and notification service

This commit is contained in:
--list
2021-06-03 11:03:27 +02:00
parent 4f76088382
commit 1f91c0ec7c
10 changed files with 2335 additions and 2235 deletions
+765 -759
View File
File diff suppressed because it is too large Load Diff
+1121 -1114
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": {
"post": {
"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}": {
"get": {
"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": {
"post": {
"operationId": "PostReturns",
@@ -1911,6 +1845,10 @@
"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": {
"get": {
"operationId": "GetShippingOptions",
@@ -2696,10 +2700,6 @@
"$ref": "#/components/schemas/product"
}
},
"usage_limit": {
"description": "The maximum number of times that a discount can be used.",
"type": "integer"
},
"created_at": {
"description": "The date with timezone at which the resource was created.",
"type": "string",
@@ -2771,6 +2771,14 @@
"$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": {
"description": "The date with timezone at which the resource was created.",
"type": "string",
@@ -3545,6 +3553,9 @@
},
"gift_card_total": {
"type": "integer"
},
"paid_total": {
"type": "integer"
}
}
},
+75 -67
View File
@@ -1147,47 +1147,6 @@ paths:
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
@@ -1238,6 +1197,47 @@ paths:
type: array
items:
$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:
post:
operationId: PostReturns
@@ -1274,6 +1274,9 @@ paths:
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:
- Return
responses:
@@ -1285,6 +1288,29 @@ paths:
properties:
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:
get:
operationId: GetShippingOptions
@@ -1346,29 +1372,6 @@ paths:
type: array
items:
$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}':
get:
operationId: GetVariantsVariant
@@ -1829,9 +1832,6 @@ components:
type: array
items:
$ref: '#/components/schemas/product'
usage_limit:
description: The maximum number of times that a discount can be used.
type: integer
created_at:
description: The date with timezone at which the resource was created.
type: string
@@ -1894,6 +1894,12 @@ components:
type: array
items:
$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:
description: The date with timezone at which the resource was created.
type: string
@@ -2527,6 +2533,8 @@ components:
type: integer
gift_card_total:
type: integer
paid_total:
type: integer
payment_provider:
title: Payment Provider
description: Represents a Payment Provider plugin and holds its installation status.
@@ -45,6 +45,9 @@ import { defaultFields, defaultRelations } from "./"
* quantity:
* description: The quantity of the Product Variant to ship.
* type: integer
* no_notification:
* description: If set to true no notification will be send.
* type: boolean
* tags:
* - Order
* responses:
@@ -65,7 +68,7 @@ export default async (req, res) => {
.items({
item_id: Validator.string().required(),
quantity: Validator.number().required(),
})
})
.required(),
return_shipping: Validator.object()
.keys({
@@ -79,6 +82,7 @@ export default async (req, res) => {
variant_id: Validator.string().required(),
quantity: Validator.number().required(),
}),
no_notification: Validator.boolean().optional()
})
const { value, error } = schema.validate(req.body)
@@ -134,6 +138,7 @@ export default async (req, res) => {
value.return_items,
value.additional_items,
value.return_shipping,
no_notification,
{ idempotency_key: idempotencyKey.idempotency_key }
)
@@ -31,6 +31,10 @@ import { MedusaError, Validator } from "medusa-core-utils"
* option_id:
* type: string
* 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:
* - Return
* responses:
@@ -59,6 +63,7 @@ export default async (req, res) => {
option_id: Validator.string().optional(),
})
.optional(),
no_notification: Validator.boolean().optional()
})
const { value, error } = schema.validate(req.body)
@@ -132,6 +137,7 @@ export default async (req, res) => {
.emit("order.return_requested", {
id: value.order_id,
return_id: createdReturn.id,
no_notification: no_notification
})
return {
@@ -1,30 +1,31 @@
import NotificationService from "../notification"
import { IdMap, MockManager, MockRepository } from "medusa-test-utils"
import { MockManager, MockRepository } from "medusa-test-utils"
describe("NotificationService", () => {
describe("send", () => {
const notificationRepository = MockRepository({ create: c => c })
const notificationRepository = MockRepository({ create: c => c })
const container = {
manager: MockManager,
notificationRepository,
noti_test: {
sendNotification: jest.fn(() =>
Promise.resolve({
to: "test@mail.com",
data: { id: "something" },
})
),
},
}
const container = {
manager: MockManager,
notificationRepository,
noti_test: {
sendNotification: jest.fn(() =>
Promise.resolve({
to: "test@mail.com",
data: { id: "something" },
})
),
},
}
const notificationService = new NotificationService(container)
beforeEach(() => {
jest.clearAllMocks()
})
beforeEach(() => {
jest.clearAllMocks()
})
describe("send", () =>{
it("successfully calls provider and saves noti", async () => {
const notificationService = new NotificationService(container)
await notificationService.send("event.test", { id: "test" }, "test")
expect(container.noti_test.sendNotification).toHaveBeenCalledTimes(1)
@@ -51,4 +52,30 @@ describe("NotificationService", () => {
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)
})
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) {
return
}
if(data['no_notification'] === true) {
return
}
return Promise.all(
subs.map(async providerId => {
+3
View File
@@ -204,6 +204,7 @@ class SwapService extends BaseService {
* the customer.
* @param {ReturnShipping?} returnShipping - an optional shipping method for
* returning the returnItems.
* @param {boolean?} noNotification - an optional flag to disable sending notification when creating swap
* @returns {Promise<Swap>} the newly created swap.
*/
async create(
@@ -211,6 +212,7 @@ class SwapService extends BaseService {
returnItems,
additionalItems,
returnShipping,
noNotification,
custom = {}
) {
return this.atomicPhase_(async manager => {
@@ -256,6 +258,7 @@ class SwapService extends BaseService {
.withTransaction(manager)
.emit(SwapService.Events.CREATED, {
id: result.id,
no_notification: noNotification,
})
return result