Merge pull request #559 from medusajs/chore/lint
chore: add better linting rules
This commit is contained in:
77 files changed
+905
-698
No files matched your search
@@ -1,9 +0,0 @@
|
||||
{
|
||||
"plugins": ["prettier"],
|
||||
"extends": ["prettier"],
|
||||
"rules": {
|
||||
"prettier/prettier": "error",
|
||||
"semi": "error",
|
||||
"no-unused-expressions": "true"
|
||||
}
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
{
|
||||
"endOfLine": "lf",
|
||||
"semi": false,
|
||||
"singleQuote": false,
|
||||
"tabWidth": 2,
|
||||
"trailingComma": "es5"
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
{
|
||||
"plugins": ["prettier"],
|
||||
"extends": ["prettier"],
|
||||
"rules": {
|
||||
"prettier/prettier": "error",
|
||||
"semi": "error",
|
||||
"no-unused-expressions": "true"
|
||||
}
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
{
|
||||
"endOfLine": "lf",
|
||||
"semi": false,
|
||||
"singleQuote": false,
|
||||
"tabWidth": 2,
|
||||
"trailingComma": "es5"
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
{
|
||||
"plugins": ["prettier"],
|
||||
"extends": ["prettier"],
|
||||
"rules": {
|
||||
"prettier/prettier": "error",
|
||||
"semi": "error",
|
||||
"no-unused-expressions": "true"
|
||||
}
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
{
|
||||
"endOfLine": "lf",
|
||||
"semi": false,
|
||||
"singleQuote": false,
|
||||
"tabWidth": 2,
|
||||
"trailingComma": "es5"
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
{
|
||||
"plugins": ["prettier"],
|
||||
"extends": ["prettier"],
|
||||
"rules": {
|
||||
"prettier/prettier": "error",
|
||||
"semi": "error",
|
||||
"no-unused-expressions": "true"
|
||||
}
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
{
|
||||
"endOfLine": "lf",
|
||||
"semi": false,
|
||||
"singleQuote": false,
|
||||
"tabWidth": 2,
|
||||
"trailingComma": "es5"
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
{
|
||||
"plugins": ["prettier"],
|
||||
"extends": ["prettier"],
|
||||
"rules": {
|
||||
"prettier/prettier": "error",
|
||||
"semi": "error",
|
||||
"no-unused-expressions": "true"
|
||||
}
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
{
|
||||
"endOfLine": "lf",
|
||||
"semi": false,
|
||||
"singleQuote": false,
|
||||
"tabWidth": 2,
|
||||
"trailingComma": "es5"
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
{
|
||||
"plugins": ["prettier"],
|
||||
"extends": ["prettier"],
|
||||
"rules": {
|
||||
"prettier/prettier": "error",
|
||||
"semi": "error",
|
||||
"no-unused-expressions": "true"
|
||||
}
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
{
|
||||
"endOfLine": "lf",
|
||||
"semi": false,
|
||||
"singleQuote": false,
|
||||
"tabWidth": 2,
|
||||
"trailingComma": "es5"
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
{
|
||||
"plugins": ["prettier"],
|
||||
"extends": ["prettier"],
|
||||
"rules": {
|
||||
"prettier/prettier": "error",
|
||||
"semi": "error",
|
||||
"no-unused-expressions": "true"
|
||||
}
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
{
|
||||
"endOfLine": "lf",
|
||||
"semi": false,
|
||||
"singleQuote": false,
|
||||
"tabWidth": 2,
|
||||
"trailingComma": "es5"
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
{
|
||||
"plugins": ["prettier"],
|
||||
"extends": ["prettier"],
|
||||
"rules": {
|
||||
"prettier/prettier": "error",
|
||||
"semi": "error",
|
||||
"no-unused-expressions": "true"
|
||||
}
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
{
|
||||
"endOfLine": "lf",
|
||||
"semi": false,
|
||||
"singleQuote": false,
|
||||
"tabWidth": 2,
|
||||
"trailingComma": "es5"
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
{
|
||||
"plugins": ["prettier"],
|
||||
"extends": ["prettier"],
|
||||
"rules": {
|
||||
"prettier/prettier": "error",
|
||||
"semi": "error",
|
||||
"no-unused-expressions": "true"
|
||||
}
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
{
|
||||
"endOfLine": "lf",
|
||||
"semi": false,
|
||||
"singleQuote": false,
|
||||
"tabWidth": 2,
|
||||
"trailingComma": "es5"
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
{
|
||||
"plugins": ["prettier"],
|
||||
"extends": ["prettier"],
|
||||
"rules": {
|
||||
"prettier/prettier": "error",
|
||||
"semi": "error",
|
||||
"no-unused-expressions": "true"
|
||||
}
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
{
|
||||
"endOfLine": "lf",
|
||||
"semi": false,
|
||||
"singleQuote": false,
|
||||
"tabWidth": 2,
|
||||
"trailingComma": "es5"
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
{
|
||||
"plugins": ["prettier"],
|
||||
"extends": ["prettier"],
|
||||
"rules": {
|
||||
"prettier/prettier": "error",
|
||||
"semi": "error",
|
||||
"no-unused-expressions": "true"
|
||||
}
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
{
|
||||
"endOfLine": "lf",
|
||||
"semi": false,
|
||||
"singleQuote": false,
|
||||
"tabWidth": 2,
|
||||
"trailingComma": "es5"
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
{
|
||||
"plugins": ["prettier"],
|
||||
"extends": ["prettier"],
|
||||
"rules": {
|
||||
"prettier/prettier": "error",
|
||||
"semi": "error",
|
||||
"no-unused-expressions": "true"
|
||||
}
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
{
|
||||
"endOfLine": "lf",
|
||||
"semi": false,
|
||||
"singleQuote": false,
|
||||
"tabWidth": 2,
|
||||
"trailingComma": "es5"
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
{
|
||||
"plugins": ["prettier"],
|
||||
"extends": ["prettier"],
|
||||
"rules": {
|
||||
"prettier/prettier": "error",
|
||||
"semi": "error",
|
||||
"no-unused-expressions": "true"
|
||||
}
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
{
|
||||
"endOfLine": "lf",
|
||||
"semi": false,
|
||||
"singleQuote": false,
|
||||
"tabWidth": 2,
|
||||
"trailingComma": "es5"
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
{
|
||||
"plugins": ["prettier"],
|
||||
"extends": ["prettier"],
|
||||
"rules": {
|
||||
"prettier/prettier": "error",
|
||||
"semi": "error",
|
||||
"no-unused-expressions": "true"
|
||||
}
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
{
|
||||
"endOfLine": "lf",
|
||||
"semi": false,
|
||||
"singleQuote": false,
|
||||
"tabWidth": 2,
|
||||
"trailingComma": "es5"
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
{
|
||||
"plugins": ["prettier"],
|
||||
"extends": ["prettier"],
|
||||
"rules": {
|
||||
"prettier/prettier": "error",
|
||||
"semi": "error",
|
||||
"no-unused-expressions": "true"
|
||||
}
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
{
|
||||
"endOfLine": "lf",
|
||||
"semi": false,
|
||||
"singleQuote": false,
|
||||
"tabWidth": 2,
|
||||
"trailingComma": "es5"
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
{
|
||||
"plugins": ["prettier"],
|
||||
"extends": ["prettier"],
|
||||
"rules": {
|
||||
"prettier/prettier": "error",
|
||||
"semi": "error",
|
||||
"no-unused-expressions": "true"
|
||||
}
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
{
|
||||
"endOfLine": "lf",
|
||||
"semi": false,
|
||||
"singleQuote": false,
|
||||
"tabWidth": 2,
|
||||
"trailingComma": "es5"
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
{
|
||||
"plugins": ["prettier"],
|
||||
"extends": ["prettier"],
|
||||
"rules": {
|
||||
"prettier/prettier": "error",
|
||||
"semi": "error",
|
||||
"no-unused-expressions": "true"
|
||||
}
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
{
|
||||
"endOfLine": "lf",
|
||||
"semi": false,
|
||||
"singleQuote": false,
|
||||
"tabWidth": 2,
|
||||
"trailingComma": "es5"
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
{
|
||||
"endOfLine": "lf",
|
||||
"semi": false,
|
||||
"singleQuote": false,
|
||||
"tabWidth": 2,
|
||||
"trailingComma": "es5"
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
{
|
||||
"plugins": ["prettier"],
|
||||
"extends": ["prettier"],
|
||||
"rules": {
|
||||
"prettier/prettier": "error",
|
||||
"semi": "error",
|
||||
"no-unused-expressions": "true"
|
||||
}
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
{
|
||||
"endOfLine": "lf",
|
||||
"semi": false,
|
||||
"singleQuote": false,
|
||||
"tabWidth": 2,
|
||||
"trailingComma": "es5"
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
{
|
||||
"endOfLine": "lf",
|
||||
"semi": false,
|
||||
"singleQuote": false,
|
||||
"tabWidth": 2,
|
||||
"trailingComma": "es5"
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
{
|
||||
"plugins": ["prettier"],
|
||||
"extends": ["prettier"],
|
||||
"rules": {
|
||||
"prettier/prettier": "error",
|
||||
"semi": "error",
|
||||
"no-unused-expressions": "true"
|
||||
}
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
{
|
||||
"endOfLine": "lf",
|
||||
"semi": false,
|
||||
"singleQuote": false,
|
||||
"tabWidth": 2,
|
||||
"trailingComma": "es5"
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
{
|
||||
"plugins": ["prettier"],
|
||||
"extends": ["prettier"],
|
||||
"rules": {
|
||||
"prettier/prettier": "error",
|
||||
"semi": "error",
|
||||
"no-unused-expressions": "true"
|
||||
}
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
{
|
||||
"endOfLine": "lf",
|
||||
"semi": false,
|
||||
"singleQuote": false,
|
||||
"tabWidth": 2,
|
||||
"trailingComma": "es5"
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
{
|
||||
"plugins": ["prettier"],
|
||||
"extends": ["prettier"],
|
||||
"rules": {
|
||||
"prettier/prettier": "error",
|
||||
"semi": "error",
|
||||
"no-unused-expressions": "true"
|
||||
}
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
{
|
||||
"endOfLine": "lf",
|
||||
"semi": false,
|
||||
"singleQuote": false,
|
||||
"tabWidth": 2,
|
||||
"trailingComma": "es5"
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
{
|
||||
"plugins": ["prettier"],
|
||||
"extends": ["prettier"],
|
||||
"rules": {
|
||||
"prettier/prettier": "error",
|
||||
"semi": "error",
|
||||
"no-unused-expressions": "true"
|
||||
}
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
{
|
||||
"endOfLine": "lf",
|
||||
"semi": false,
|
||||
"singleQuote": false,
|
||||
"tabWidth": 2,
|
||||
"trailingComma": "es5"
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
{
|
||||
"plugins": ["prettier"],
|
||||
"extends": ["prettier"],
|
||||
"rules": {
|
||||
"prettier/prettier": "error",
|
||||
"semi": "error",
|
||||
"no-unused-expressions": "true"
|
||||
}
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
{
|
||||
"endOfLine": "lf",
|
||||
"semi": false,
|
||||
"singleQuote": false,
|
||||
"tabWidth": 2,
|
||||
"trailingComma": "es5"
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
{
|
||||
"plugins": ["prettier"],
|
||||
"extends": ["prettier"],
|
||||
"rules": {
|
||||
"prettier/prettier": "error",
|
||||
"semi": "error",
|
||||
"no-unused-expressions": "true"
|
||||
}
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
{
|
||||
"endOfLine": "lf",
|
||||
"semi": false,
|
||||
"singleQuote": false,
|
||||
"tabWidth": 2,
|
||||
"trailingComma": "es5"
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
{
|
||||
"plugins": ["prettier"],
|
||||
"extends": ["prettier"],
|
||||
"rules": {
|
||||
"prettier/prettier": "error",
|
||||
"semi": "error",
|
||||
"no-unused-expressions": "true"
|
||||
}
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
{
|
||||
"endOfLine": "lf",
|
||||
"semi": false,
|
||||
"singleQuote": false,
|
||||
"tabWidth": 2,
|
||||
"trailingComma": "es5"
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
{
|
||||
"plugins": ["prettier"],
|
||||
"extends": ["prettier"],
|
||||
"rules": {
|
||||
"prettier/prettier": "error",
|
||||
"semi": "error",
|
||||
"no-unused-expressions": "true"
|
||||
}
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
{
|
||||
"plugins": ["prettier"],
|
||||
"extends": ["prettier"],
|
||||
"rules": {
|
||||
"prettier/prettier": "error",
|
||||
"semi": "error",
|
||||
"no-unused-expressions": "true"
|
||||
}
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
{
|
||||
"endOfLine": "lf",
|
||||
"semi": false,
|
||||
"singleQuote": false,
|
||||
"tabWidth": 2,
|
||||
"trailingComma": "es5"
|
||||
}
|
||||
@@ -3,7 +3,7 @@ import { BaseService } from "medusa-interfaces"
|
||||
|
||||
/**
|
||||
* Can authenticate a user based on email password combination
|
||||
* @implements BaseService
|
||||
* @extends BaseService
|
||||
*/
|
||||
class AuthService extends BaseService {
|
||||
constructor({ userService, customerService }) {
|
||||
@@ -47,7 +47,9 @@ class AuthService extends BaseService {
|
||||
success: true,
|
||||
user,
|
||||
}
|
||||
} catch (error) {}
|
||||
} catch (error) {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
import _ from "lodash"
|
||||
import { Validator, MedusaError } from "medusa-core-utils"
|
||||
import { MedusaError } from "medusa-core-utils"
|
||||
import { BaseService } from "medusa-interfaces"
|
||||
import { Brackets } from "typeorm"
|
||||
|
||||
class ClaimService extends BaseService {
|
||||
static Events = {
|
||||
@@ -102,7 +100,7 @@ class ClaimService extends BaseService {
|
||||
}
|
||||
|
||||
update(id, data) {
|
||||
return this.atomicPhase_(async manager => {
|
||||
return this.atomicPhase_(async (manager) => {
|
||||
const claimRepo = manager.getCustomRepository(this.claimRepository_)
|
||||
const claim = await this.retrieve(id, { relations: ["shipping_methods"] })
|
||||
|
||||
@@ -113,13 +111,7 @@ class ClaimService extends BaseService {
|
||||
)
|
||||
}
|
||||
|
||||
const {
|
||||
claim_items,
|
||||
shipping_methods,
|
||||
metadata,
|
||||
fulfillment_status,
|
||||
no_notification,
|
||||
} = data
|
||||
const { claim_items, shipping_methods, metadata, no_notification } = data
|
||||
|
||||
if (metadata) {
|
||||
claim.metadata = this.setMetadata_(claim, metadata)
|
||||
@@ -183,9 +175,11 @@ class ClaimService extends BaseService {
|
||||
* Creates a Claim on an Order. Claims consists of items that are claimed and
|
||||
* optionally items to be sent as replacement for the claimed items. The
|
||||
* shipping address that the new items will be shipped to
|
||||
* @param {Object} data - the object containing all data required to create a claim
|
||||
* @return {Object} created claim
|
||||
*/
|
||||
create(data) {
|
||||
return this.atomicPhase_(async manager => {
|
||||
return this.atomicPhase_(async (manager) => {
|
||||
const claimRepo = manager.getCustomRepository(this.claimRepository_)
|
||||
|
||||
const {
|
||||
@@ -257,8 +251,8 @@ class ClaimService extends BaseService {
|
||||
|
||||
let toRefund = refund_amount
|
||||
if (type === "refund" && typeof refund_amount === "undefined") {
|
||||
const lines = claim_items.map(ci => {
|
||||
const orderItem = order.items.find(oi => oi.id === ci.item_id)
|
||||
const lines = claim_items.map((ci) => {
|
||||
const orderItem = order.items.find((oi) => oi.id === ci.item_id)
|
||||
return {
|
||||
...orderItem,
|
||||
quantity: ci.quantity,
|
||||
@@ -274,7 +268,7 @@ class ClaimService extends BaseService {
|
||||
}
|
||||
|
||||
const newItems = await Promise.all(
|
||||
additional_items.map(i =>
|
||||
additional_items.map((i) =>
|
||||
this.lineItemService_
|
||||
.withTransaction(manager)
|
||||
.generate(i.variant_id, order.region_id, i.quantity)
|
||||
@@ -332,7 +326,7 @@ class ClaimService extends BaseService {
|
||||
await this.returnService_.withTransaction(manager).create({
|
||||
order_id: order.id,
|
||||
claim_order_id: result.id,
|
||||
items: claim_items.map(ci => ({
|
||||
items: claim_items.map((ci) => ({
|
||||
item_id: ci.item_id,
|
||||
quantity: ci.quantity,
|
||||
metadata: ci.metadata,
|
||||
@@ -362,7 +356,7 @@ class ClaimService extends BaseService {
|
||||
) {
|
||||
const { metadata, no_notification } = config
|
||||
|
||||
return this.atomicPhase_(async manager => {
|
||||
return this.atomicPhase_(async (manager) => {
|
||||
const claim = await this.retrieve(id, {
|
||||
relations: [
|
||||
"additional_items",
|
||||
@@ -429,7 +423,7 @@ class ClaimService extends BaseService {
|
||||
is_claim: true,
|
||||
no_notification: evaluatedNoNotification,
|
||||
},
|
||||
claim.additional_items.map(i => ({
|
||||
claim.additional_items.map((i) => ({
|
||||
item_id: i.id,
|
||||
quantity: i.quantity,
|
||||
})),
|
||||
@@ -445,7 +439,7 @@ class ClaimService extends BaseService {
|
||||
|
||||
for (const item of claim.additional_items) {
|
||||
const fulfillmentItem = successfullyFulfilled.find(
|
||||
f => item.id === f.item_id
|
||||
(f) => item.id === f.item_id
|
||||
)
|
||||
|
||||
if (fulfillmentItem) {
|
||||
@@ -485,7 +479,7 @@ class ClaimService extends BaseService {
|
||||
}
|
||||
|
||||
async cancelFulfillment(fulfillmentId) {
|
||||
return this.atomicPhase_(async manager => {
|
||||
return this.atomicPhase_(async (manager) => {
|
||||
const canceled = await this.fulfillmentService_
|
||||
.withTransaction(manager)
|
||||
.cancelFulfillment(fulfillmentId)
|
||||
@@ -508,7 +502,7 @@ class ClaimService extends BaseService {
|
||||
}
|
||||
|
||||
async processRefund(id) {
|
||||
return this.atomicPhase_(async manager => {
|
||||
return this.atomicPhase_(async (manager) => {
|
||||
const claim = await this.retrieve(id, {
|
||||
relations: ["order", "order.payments"],
|
||||
})
|
||||
@@ -560,7 +554,7 @@ class ClaimService extends BaseService {
|
||||
) {
|
||||
const { metadata, no_notification } = config
|
||||
|
||||
return this.atomicPhase_(async manager => {
|
||||
return this.atomicPhase_(async (manager) => {
|
||||
const claim = await this.retrieve(id, {
|
||||
relations: ["additional_items"],
|
||||
})
|
||||
@@ -584,7 +578,7 @@ class ClaimService extends BaseService {
|
||||
claim.fulfillment_status = "shipped"
|
||||
|
||||
for (const i of claim.additional_items) {
|
||||
const shipped = shipment.items.find(si => si.item_id === i.id)
|
||||
const shipped = shipment.items.find((si) => si.item_id === i.id)
|
||||
if (shipped) {
|
||||
const shippedQty = (i.shipped_quantity || 0) + shipped.quantity
|
||||
await this.lineItemService_.withTransaction(manager).update(i.id, {
|
||||
@@ -617,7 +611,7 @@ class ClaimService extends BaseService {
|
||||
}
|
||||
|
||||
async cancel(id) {
|
||||
return this.atomicPhase_(async manager => {
|
||||
return this.atomicPhase_(async (manager) => {
|
||||
const claim = await this.retrieve(id, {
|
||||
relations: ["return_order", "fulfillments", "order", "order.refunds"],
|
||||
})
|
||||
@@ -665,6 +659,7 @@ class ClaimService extends BaseService {
|
||||
|
||||
/**
|
||||
* @param {Object} selector - the query object for find
|
||||
* @param {Object} config - the config object containing query settings
|
||||
* @return {Promise} the result of the find operation
|
||||
*/
|
||||
async list(
|
||||
@@ -678,7 +673,8 @@ class ClaimService extends BaseService {
|
||||
|
||||
/**
|
||||
* Gets an order by id.
|
||||
* @param {string} orderId - id of order to retrieve
|
||||
* @param {string} claimId - id of order to retrieve
|
||||
* @param {Object} config - the config object containing query settings
|
||||
* @return {Promise<Order>} the order document
|
||||
*/
|
||||
async retrieve(claimId, config = {}) {
|
||||
@@ -717,7 +713,7 @@ class ClaimService extends BaseService {
|
||||
const keyPath = `metadata.${key}`
|
||||
return this.orderModel_
|
||||
.updateOne({ _id: validatedId }, { $unset: { [keyPath]: "" } })
|
||||
.catch(err => {
|
||||
.catch((err) => {
|
||||
throw new MedusaError(MedusaError.Types.DB_ERROR, err.message)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1,14 +1,11 @@
|
||||
import _ from "lodash"
|
||||
import randomize from "randomatic"
|
||||
import { BaseService } from "medusa-interfaces"
|
||||
import { Validator, MedusaError } from "medusa-core-utils"
|
||||
import { MedusaErrorCodes } from "medusa-core-utils/dist/errors"
|
||||
import { parse, toSeconds } from "iso8601-duration"
|
||||
import { Brackets, ILike } from "typeorm"
|
||||
|
||||
/**
|
||||
* Provides layer to manipulate discounts.
|
||||
* @implements BaseService
|
||||
* @implements {BaseService}
|
||||
*/
|
||||
class DiscountService extends BaseService {
|
||||
constructor({
|
||||
@@ -17,7 +14,6 @@ class DiscountService extends BaseService {
|
||||
discountRuleRepository,
|
||||
giftCardRepository,
|
||||
totalsService,
|
||||
productVariantService,
|
||||
productService,
|
||||
regionService,
|
||||
eventBusService,
|
||||
@@ -80,21 +76,13 @@ class DiscountService extends BaseService {
|
||||
id: Validator.string().optional(),
|
||||
description: Validator.string().optional(),
|
||||
type: Validator.string().required(),
|
||||
value: Validator.number()
|
||||
.min(0)
|
||||
.required(),
|
||||
value: Validator.number().min(0).required(),
|
||||
allocation: Validator.string().required(),
|
||||
valid_for: Validator.array().optional(),
|
||||
created_at: Validator.date().optional(),
|
||||
updated_at: Validator.date()
|
||||
.allow(null)
|
||||
.optional(),
|
||||
deleted_at: Validator.date()
|
||||
.allow(null)
|
||||
.optional(),
|
||||
metadata: Validator.object()
|
||||
.allow(null)
|
||||
.optional(),
|
||||
updated_at: Validator.date().allow(null).optional(),
|
||||
deleted_at: Validator.date().allow(null).optional(),
|
||||
metadata: Validator.object().allow(null).optional(),
|
||||
})
|
||||
|
||||
const { value, error } = schema.validate(discountRule)
|
||||
@@ -117,6 +105,7 @@ class DiscountService extends BaseService {
|
||||
|
||||
/**
|
||||
* @param {Object} selector - the query object for find
|
||||
* @param {Object} config - the config object containing query settings
|
||||
* @return {Promise} the result of the find operation
|
||||
*/
|
||||
async list(selector = {}, config = { relations: [], skip: 0, take: 10 }) {
|
||||
@@ -130,6 +119,7 @@ class DiscountService extends BaseService {
|
||||
|
||||
/**
|
||||
* @param {Object} selector - the query object for find
|
||||
* @param {Object} config - the config object containing query settings
|
||||
* @return {Promise} the result of the find operation
|
||||
*/
|
||||
async listAndCount(
|
||||
@@ -153,11 +143,11 @@ class DiscountService extends BaseService {
|
||||
|
||||
delete where.code
|
||||
|
||||
query.where = qb => {
|
||||
query.where = (qb) => {
|
||||
qb.where(where)
|
||||
|
||||
qb.andWhere(
|
||||
new Brackets(qb => {
|
||||
new Brackets((qb) => {
|
||||
qb.where({ code: ILike(`%${q}%`) })
|
||||
})
|
||||
)
|
||||
@@ -176,19 +166,19 @@ class DiscountService extends BaseService {
|
||||
* @return {Promise} the result of the create operation
|
||||
*/
|
||||
async create(discount) {
|
||||
return this.atomicPhase_(async manager => {
|
||||
return this.atomicPhase_(async (manager) => {
|
||||
const discountRepo = manager.getCustomRepository(this.discountRepository_)
|
||||
const ruleRepo = manager.getCustomRepository(this.discountRuleRepository_)
|
||||
|
||||
if (discount.rule?.valid_for) {
|
||||
discount.rule.valid_for = discount.rule.valid_for.map(id => ({ id }))
|
||||
discount.rule.valid_for = discount.rule.valid_for.map((id) => ({ id }))
|
||||
}
|
||||
|
||||
const validatedRule = this.validateDiscountRule_(discount.rule)
|
||||
|
||||
if (discount.regions) {
|
||||
discount.regions = await Promise.all(
|
||||
discount.regions.map(regionId =>
|
||||
discount.regions.map((regionId) =>
|
||||
this.regionService_.withTransaction(manager).retrieve(regionId)
|
||||
)
|
||||
)
|
||||
@@ -209,6 +199,7 @@ class DiscountService extends BaseService {
|
||||
/**
|
||||
* Gets a discount by id.
|
||||
* @param {string} discountId - id of discount to retrieve
|
||||
* @param {Object} config - the config object containing query settings
|
||||
* @return {Promise<Discount>} the discount
|
||||
*/
|
||||
async retrieve(discountId, config = {}) {
|
||||
@@ -233,6 +224,7 @@ class DiscountService extends BaseService {
|
||||
/**
|
||||
* Gets a discount by discount code.
|
||||
* @param {string} discountCode - discount code of discount to retrieve
|
||||
* @param {array} relations - list of relations
|
||||
* @return {Promise<Discount>} the discount document
|
||||
*/
|
||||
async retrieveByCode(discountCode, relations = []) {
|
||||
@@ -269,7 +261,7 @@ class DiscountService extends BaseService {
|
||||
* @return {Promise} the result of the update operation
|
||||
*/
|
||||
async update(discountId, update) {
|
||||
return this.atomicPhase_(async manager => {
|
||||
return this.atomicPhase_(async (manager) => {
|
||||
const discountRepo = manager.getCustomRepository(this.discountRepository_)
|
||||
|
||||
const discount = await this.retrieve(discountId)
|
||||
@@ -287,7 +279,7 @@ class DiscountService extends BaseService {
|
||||
|
||||
if (regions) {
|
||||
discount.regions = await Promise.all(
|
||||
regions.map(regionId => this.regionService_.retrieve(regionId))
|
||||
regions.map((regionId) => this.regionService_.retrieve(regionId))
|
||||
)
|
||||
}
|
||||
|
||||
@@ -298,7 +290,9 @@ class DiscountService extends BaseService {
|
||||
if (rule) {
|
||||
discount.rule = this.validateDiscountRule_(rule)
|
||||
if (rule.valid_for) {
|
||||
discount.rule.valid_for = discount.rule.valid_for.map(id => ({ id }))
|
||||
discount.rule.valid_for = discount.rule.valid_for.map((id) => ({
|
||||
id,
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -314,11 +308,11 @@ class DiscountService extends BaseService {
|
||||
/**
|
||||
* Creates a dynamic code for a discount id.
|
||||
* @param {string} discountId - the id of the discount to create a code for
|
||||
* @param {string} code - the code to identify the discount by
|
||||
* @param {Object} data - the object containing a code to identify the discount by
|
||||
* @return {Promise} the newly created dynamic code
|
||||
*/
|
||||
async createDynamicCode(discountId, data) {
|
||||
return this.atomicPhase_(async manager => {
|
||||
return this.atomicPhase_(async (manager) => {
|
||||
const discountRepo = manager.getCustomRepository(this.discountRepository_)
|
||||
|
||||
const discount = await this.retrieve(discountId)
|
||||
@@ -367,13 +361,15 @@ class DiscountService extends BaseService {
|
||||
* @return {Promise} the newly created dynamic code
|
||||
*/
|
||||
async deleteDynamicCode(discountId, code) {
|
||||
return this.atomicPhase_(async manager => {
|
||||
return this.atomicPhase_(async (manager) => {
|
||||
const discountRepo = manager.getCustomRepository(this.discountRepository_)
|
||||
const discount = await discountRepo.findOne({
|
||||
where: { parent_discount_id: discountId, code },
|
||||
})
|
||||
|
||||
if (!discount) return Promise.resolve()
|
||||
if (!discount) {
|
||||
return Promise.resolve()
|
||||
}
|
||||
|
||||
await discountRepo.softRemove(discount)
|
||||
|
||||
@@ -388,7 +384,7 @@ class DiscountService extends BaseService {
|
||||
* @return {Promise} the result of the update operation
|
||||
*/
|
||||
async addValidProduct(discountId, productId) {
|
||||
return this.atomicPhase_(async manager => {
|
||||
return this.atomicPhase_(async (manager) => {
|
||||
const discountRuleRepo = manager.getCustomRepository(
|
||||
this.discountRuleRepository_
|
||||
)
|
||||
@@ -399,7 +395,7 @@ class DiscountService extends BaseService {
|
||||
|
||||
const { rule } = discount
|
||||
|
||||
const exists = rule.valid_for.find(p => p.id === productId)
|
||||
const exists = rule.valid_for.find((p) => p.id === productId)
|
||||
// If product is already present, we return early
|
||||
if (exists) {
|
||||
return rule
|
||||
@@ -421,7 +417,7 @@ class DiscountService extends BaseService {
|
||||
* @return {Promise} the result of the update operation
|
||||
*/
|
||||
async removeValidProduct(discountId, productId) {
|
||||
return this.atomicPhase_(async manager => {
|
||||
return this.atomicPhase_(async (manager) => {
|
||||
const discountRuleRepo = manager.getCustomRepository(
|
||||
this.discountRuleRepository_
|
||||
)
|
||||
@@ -432,13 +428,13 @@ class DiscountService extends BaseService {
|
||||
|
||||
const { rule } = discount
|
||||
|
||||
const exists = rule.valid_for.find(p => p.id === productId)
|
||||
const exists = rule.valid_for.find((p) => p.id === productId)
|
||||
// If product is not present, we return early
|
||||
if (!exists) {
|
||||
return rule
|
||||
}
|
||||
|
||||
rule.valid_for = rule.valid_for.filter(p => p.id !== productId)
|
||||
rule.valid_for = rule.valid_for.filter((p) => p.id !== productId)
|
||||
|
||||
const updated = await discountRuleRepo.save(rule)
|
||||
return updated
|
||||
@@ -452,14 +448,14 @@ class DiscountService extends BaseService {
|
||||
* @return {Promise} the result of the update operation
|
||||
*/
|
||||
async addRegion(discountId, regionId) {
|
||||
return this.atomicPhase_(async manager => {
|
||||
return this.atomicPhase_(async (manager) => {
|
||||
const discountRepo = manager.getCustomRepository(this.discountRepository_)
|
||||
|
||||
const discount = await this.retrieve(discountId, {
|
||||
relations: ["regions"],
|
||||
})
|
||||
|
||||
const exists = discount.regions.find(r => r.id === regionId)
|
||||
const exists = discount.regions.find((r) => r.id === regionId)
|
||||
// If region is already present, we return early
|
||||
if (exists) {
|
||||
return discount
|
||||
@@ -481,20 +477,20 @@ class DiscountService extends BaseService {
|
||||
* @return {Promise} the result of the update operation
|
||||
*/
|
||||
async removeRegion(discountId, regionId) {
|
||||
return this.atomicPhase_(async manager => {
|
||||
return this.atomicPhase_(async (manager) => {
|
||||
const discountRepo = manager.getCustomRepository(this.discountRepository_)
|
||||
|
||||
const discount = await this.retrieve(discountId, {
|
||||
relations: ["regions"],
|
||||
})
|
||||
|
||||
const exists = discount.regions.find(r => r.id === regionId)
|
||||
const exists = discount.regions.find((r) => r.id === regionId)
|
||||
// If region is not present, we return early
|
||||
if (!exists) {
|
||||
return discount
|
||||
}
|
||||
|
||||
discount.regions = discount.regions.filter(r => r.id !== regionId)
|
||||
discount.regions = discount.regions.filter((r) => r.id !== regionId)
|
||||
|
||||
const updated = await discountRepo.save(discount)
|
||||
return updated
|
||||
@@ -507,12 +503,14 @@ class DiscountService extends BaseService {
|
||||
* @return {Promise} the result of the delete operation
|
||||
*/
|
||||
async delete(discountId) {
|
||||
return this.atomicPhase_(async manager => {
|
||||
return this.atomicPhase_(async (manager) => {
|
||||
const discountRepo = manager.getCustomRepository(this.discountRepository_)
|
||||
|
||||
const discount = await discountRepo.findOne({ where: { id: discountId } })
|
||||
|
||||
if (!discount) return Promise.resolve()
|
||||
if (!discount) {
|
||||
return Promise.resolve()
|
||||
}
|
||||
|
||||
await discountRepo.softRemove(discount)
|
||||
|
||||
@@ -522,7 +520,7 @@ class DiscountService extends BaseService {
|
||||
|
||||
/**
|
||||
* Decorates a discount.
|
||||
* @param {Discount} discount - the discount to decorate.
|
||||
* @param {string} discountId - id of discount to decorate
|
||||
* @param {string[]} fields - the fields to include.
|
||||
* @param {string[]} expandFields - fields to expand.
|
||||
* @return {Discount} return the decorated discount.
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
import _ from "lodash"
|
||||
import { BaseService } from "medusa-interfaces"
|
||||
import { MedusaError } from "medusa-core-utils"
|
||||
|
||||
/**
|
||||
* Handles Fulfillments
|
||||
* @implements BaseService
|
||||
* @extends BaseService
|
||||
*/
|
||||
class FulfillmentService extends BaseService {
|
||||
constructor({
|
||||
@@ -61,7 +60,7 @@ class FulfillmentService extends BaseService {
|
||||
}
|
||||
|
||||
partitionItems_(shippingMethods, items) {
|
||||
let partitioned = []
|
||||
const partitioned = []
|
||||
// partition order items to their dedicated shipping method
|
||||
for (const method of shippingMethods) {
|
||||
const temp = { shipping_method: method }
|
||||
@@ -95,12 +94,12 @@ class FulfillmentService extends BaseService {
|
||||
async getFulfillmentItems_(order, items, transformer) {
|
||||
const toReturn = await Promise.all(
|
||||
items.map(async ({ item_id, quantity }) => {
|
||||
const item = order.items.find(i => i.id === item_id)
|
||||
const item = order.items.find((i) => i.id === item_id)
|
||||
return transformer(item, quantity)
|
||||
})
|
||||
)
|
||||
|
||||
return toReturn.filter(i => !!i)
|
||||
return toReturn.filter((i) => !!i)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -137,6 +136,7 @@ class FulfillmentService extends BaseService {
|
||||
/**
|
||||
* Retrieves a fulfillment by its id.
|
||||
* @param {string} id - the id of the fulfillment to retrieve
|
||||
* @param {object} config - optional values to include with fulfillmentRepository query
|
||||
* @return {Fulfillment} the fulfillment
|
||||
*/
|
||||
async retrieve(id, config = {}) {
|
||||
@@ -165,11 +165,11 @@ class FulfillmentService extends BaseService {
|
||||
* those partitions.
|
||||
* @param {Order} order - order to create fulfillment for
|
||||
* @param {{ item_id: string, quantity: number}[]} itemsToFulfill - the items in the order to fulfill
|
||||
* @param {object} metadata - potential metadata to add
|
||||
* @param {object} custom - potential custom values to add
|
||||
* @return {Fulfillment[]} the created fulfillments
|
||||
*/
|
||||
async createFulfillment(order, itemsToFulfill, custom = {}) {
|
||||
return this.atomicPhase_(async manager => {
|
||||
return this.atomicPhase_(async (manager) => {
|
||||
const fulfillmentRepository = manager.getCustomRepository(
|
||||
this.fulfillmentRepository_
|
||||
)
|
||||
@@ -190,18 +190,19 @@ class FulfillmentService extends BaseService {
|
||||
const ful = fulfillmentRepository.create({
|
||||
...custom,
|
||||
provider_id: shipping_method.shipping_option.provider_id,
|
||||
items: items.map(i => ({ item_id: i.id, quantity: i.quantity })),
|
||||
items: items.map((i) => ({ item_id: i.id, quantity: i.quantity })),
|
||||
data: {},
|
||||
})
|
||||
|
||||
let result = await fulfillmentRepository.save(ful)
|
||||
const result = await fulfillmentRepository.save(ful)
|
||||
|
||||
result.data = await this.fulfillmentProviderService_.createFulfillment(
|
||||
shipping_method,
|
||||
items,
|
||||
{ ...order },
|
||||
{ ...result }
|
||||
)
|
||||
result.data =
|
||||
await this.fulfillmentProviderService_.createFulfillment(
|
||||
shipping_method,
|
||||
items,
|
||||
{ ...order },
|
||||
{ ...result }
|
||||
)
|
||||
|
||||
return fulfillmentRepository.save(result)
|
||||
})
|
||||
@@ -220,7 +221,7 @@ class FulfillmentService extends BaseService {
|
||||
*
|
||||
*/
|
||||
cancelFulfillment(fulfillmentOrId) {
|
||||
return this.atomicPhase_(async manager => {
|
||||
return this.atomicPhase_(async (manager) => {
|
||||
let id = fulfillmentOrId
|
||||
if (typeof fulfillmentOrId === "object") {
|
||||
id = fulfillmentOrId.id
|
||||
@@ -260,9 +261,9 @@ class FulfillmentService extends BaseService {
|
||||
|
||||
/**
|
||||
* Creates a shipment by marking a fulfillment as shipped. Adds
|
||||
* tracking numbers and potentially more metadata.
|
||||
* tracking links and potentially more metadata.
|
||||
* @param {Order} fulfillmentId - the fulfillment to ship
|
||||
* @param {TrackingLink[]} trackingNumbers - tracking numbers for the shipment
|
||||
* @param {TrackingLink[]} trackingLinks - tracking links for the shipment
|
||||
* @param {object} config - potential configuration settings, such as no_notification and metadata
|
||||
* @return {Fulfillment} the shipped fulfillment
|
||||
*/
|
||||
@@ -276,7 +277,7 @@ class FulfillmentService extends BaseService {
|
||||
) {
|
||||
const { metadata, no_notification } = config
|
||||
|
||||
return this.atomicPhase_(async manager => {
|
||||
return this.atomicPhase_(async (manager) => {
|
||||
const fulfillmentRepository = manager.getCustomRepository(
|
||||
this.fulfillmentRepository_
|
||||
)
|
||||
@@ -298,7 +299,7 @@ class FulfillmentService extends BaseService {
|
||||
const now = new Date()
|
||||
fulfillment.shipped_at = now
|
||||
|
||||
fulfillment.tracking_links = trackingLinks.map(tl =>
|
||||
fulfillment.tracking_links = trackingLinks.map((tl) =>
|
||||
trackingLinkRepo.create(tl)
|
||||
)
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ import { Brackets } from "typeorm"
|
||||
|
||||
/**
|
||||
* Provides layer to manipulate gift cards.
|
||||
* @implements BaseService
|
||||
* @extends BaseService
|
||||
*/
|
||||
class GiftCardService extends BaseService {
|
||||
static Events = {
|
||||
@@ -101,7 +101,7 @@ class GiftCardService extends BaseService {
|
||||
.select(["gift_card.id"])
|
||||
.where(where)
|
||||
.andWhere(
|
||||
new Brackets(qb => {
|
||||
new Brackets((qb) => {
|
||||
return qb
|
||||
.where(`gift_card.code ILIKE :q`, { q: `%${q}%` })
|
||||
.orWhere(`display_id::varchar(255) ILIKE :dId`, { dId: `${q}` })
|
||||
@@ -111,14 +111,14 @@ class GiftCardService extends BaseService {
|
||||
|
||||
return giftCardRepo.findWithRelations(
|
||||
rels,
|
||||
raw.map(i => i.id)
|
||||
raw.map((i) => i.id)
|
||||
)
|
||||
}
|
||||
return giftCardRepo.findWithRelations(rels, query)
|
||||
}
|
||||
|
||||
async createTransaction(data) {
|
||||
return this.atomicPhase_(async manager => {
|
||||
return this.atomicPhase_(async (manager) => {
|
||||
const gctRepo = manager.getCustomRepository(this.giftCardTransactionRepo_)
|
||||
const created = gctRepo.create(data)
|
||||
const saved = await gctRepo.save(created)
|
||||
@@ -132,7 +132,7 @@ class GiftCardService extends BaseService {
|
||||
* @return {Promise<GiftCard>} the result of the create operation
|
||||
*/
|
||||
async create(giftCard) {
|
||||
return this.atomicPhase_(async manager => {
|
||||
return this.atomicPhase_(async (manager) => {
|
||||
const giftCardRepo = manager.getCustomRepository(this.giftCardRepository_)
|
||||
|
||||
if (!giftCard.region_id) {
|
||||
@@ -169,6 +169,7 @@ class GiftCardService extends BaseService {
|
||||
/**
|
||||
* Gets a gift card by id.
|
||||
* @param {string} giftCardId - id of gift card to retrieve
|
||||
* @param {object} config - optional values to include with gift card query
|
||||
* @return {Promise<GiftCard>} the gift card
|
||||
*/
|
||||
async retrieve(giftCardId, config = {}) {
|
||||
@@ -244,7 +245,7 @@ class GiftCardService extends BaseService {
|
||||
* @return {Promise} the result of the update operation
|
||||
*/
|
||||
async update(giftCardId, update) {
|
||||
return this.atomicPhase_(async manager => {
|
||||
return this.atomicPhase_(async (manager) => {
|
||||
const giftCardRepo = manager.getCustomRepository(this.giftCardRepository_)
|
||||
|
||||
const giftCard = await this.retrieve(giftCardId)
|
||||
@@ -285,12 +286,14 @@ class GiftCardService extends BaseService {
|
||||
* @return {Promise} the result of the delete operation
|
||||
*/
|
||||
async delete(giftCardId) {
|
||||
return this.atomicPhase_(async manager => {
|
||||
return this.atomicPhase_(async (manager) => {
|
||||
const giftCardRepo = manager.getCustomRepository(this.giftCardRepository_)
|
||||
|
||||
const giftCard = await giftCardRepo.findOne({ where: { id: giftCardId } })
|
||||
|
||||
if (!giftCard) return Promise.resolve()
|
||||
if (!giftCard) {
|
||||
return Promise.resolve()
|
||||
}
|
||||
|
||||
await giftCardRepo.softRemove(giftCard)
|
||||
|
||||
|
||||
@@ -178,6 +178,7 @@ class OrderService extends BaseService {
|
||||
|
||||
/**
|
||||
* @param {Object} selector - the query object for find
|
||||
* @param {Object} config - the config to be used for find
|
||||
* @return {Promise} the result of the find operation
|
||||
*/
|
||||
async list(
|
||||
@@ -187,9 +188,8 @@ class OrderService extends BaseService {
|
||||
const orderRepo = this.manager_.getCustomRepository(this.orderRepository_)
|
||||
const query = this.buildQuery_(selector, config)
|
||||
|
||||
const { select, relations, totalsToSelect } = this.transformQueryForTotals_(
|
||||
config
|
||||
)
|
||||
const { select, relations, totalsToSelect } =
|
||||
this.transformQueryForTotals_(config)
|
||||
|
||||
if (select && select.length) {
|
||||
query.select = select
|
||||
@@ -201,7 +201,7 @@ class OrderService extends BaseService {
|
||||
|
||||
const raw = await orderRepo.find(query)
|
||||
|
||||
return raw.map(r => this.decorateTotals_(r, totalsToSelect))
|
||||
return raw.map((r) => this.decorateTotals_(r, totalsToSelect))
|
||||
}
|
||||
|
||||
async listAndCount(
|
||||
@@ -231,11 +231,11 @@ class OrderService extends BaseService {
|
||||
},
|
||||
}
|
||||
|
||||
query.where = qb => {
|
||||
query.where = (qb) => {
|
||||
qb.where(where)
|
||||
|
||||
qb.andWhere(
|
||||
new Brackets(qb => {
|
||||
new Brackets((qb) => {
|
||||
qb.where(`shipping_address.first_name ILIKE :qfn`, {
|
||||
qfn: `%${q}%`,
|
||||
})
|
||||
@@ -246,20 +246,19 @@ class OrderService extends BaseService {
|
||||
}
|
||||
}
|
||||
|
||||
const { select, relations, totalsToSelect } = this.transformQueryForTotals_(
|
||||
config
|
||||
)
|
||||
const { select, relations, totalsToSelect } =
|
||||
this.transformQueryForTotals_(config)
|
||||
|
||||
if (select && select.length) {
|
||||
query.select = select
|
||||
}
|
||||
|
||||
let rels = relations
|
||||
const rels = relations
|
||||
delete query.relations
|
||||
|
||||
const raw = await orderRepo.findWithRelations(rels, query)
|
||||
const count = await orderRepo.count(query)
|
||||
const orders = raw.map(r => this.decorateTotals_(r, totalsToSelect))
|
||||
const orders = raw.map((r) => this.decorateTotals_(r, totalsToSelect))
|
||||
|
||||
return [orders, count]
|
||||
}
|
||||
@@ -289,7 +288,7 @@ class OrderService extends BaseService {
|
||||
"swaps.additional_items.refundable",
|
||||
]
|
||||
|
||||
const totalsToSelect = select.filter(v => totalFields.includes(v))
|
||||
const totalsToSelect = select.filter((v) => totalFields.includes(v))
|
||||
if (totalsToSelect.length > 0) {
|
||||
const relationSet = new Set(relations)
|
||||
relationSet.add("items")
|
||||
@@ -305,7 +304,7 @@ class OrderService extends BaseService {
|
||||
relationSet.add("region")
|
||||
relations = [...relationSet]
|
||||
|
||||
select = select.filter(v => !totalFields.includes(v))
|
||||
select = select.filter((v) => !totalFields.includes(v))
|
||||
}
|
||||
|
||||
return {
|
||||
@@ -318,15 +317,15 @@ class OrderService extends BaseService {
|
||||
/**
|
||||
* Gets an order by id.
|
||||
* @param {string} orderId - id of order to retrieve
|
||||
* @param {Object} config - config of order to retrieve
|
||||
* @return {Promise<Order>} the order document
|
||||
*/
|
||||
async retrieve(orderId, config = {}) {
|
||||
const orderRepo = this.manager_.getCustomRepository(this.orderRepository_)
|
||||
const validatedId = this.validateId_(orderId)
|
||||
|
||||
const { select, relations, totalsToSelect } = this.transformQueryForTotals_(
|
||||
config
|
||||
)
|
||||
const { select, relations, totalsToSelect } =
|
||||
this.transformQueryForTotals_(config)
|
||||
|
||||
const query = {
|
||||
where: { id: validatedId },
|
||||
@@ -357,14 +356,14 @@ class OrderService extends BaseService {
|
||||
/**
|
||||
* Gets an order by cart id.
|
||||
* @param {string} cartId - cart id to find order
|
||||
* @param {Object} config - the config to be used to find order
|
||||
* @return {Promise<Order>} the order document
|
||||
*/
|
||||
async retrieveByCartId(cartId, config = {}) {
|
||||
const orderRepo = this.manager_.getCustomRepository(this.orderRepository_)
|
||||
|
||||
const { select, relations, totalsToSelect } = this.transformQueryForTotals_(
|
||||
config
|
||||
)
|
||||
const { select, relations, totalsToSelect } =
|
||||
this.transformQueryForTotals_(config)
|
||||
|
||||
const query = {
|
||||
where: { cart_id: cartId },
|
||||
@@ -397,7 +396,7 @@ class OrderService extends BaseService {
|
||||
* @return {Promise<Order>} the order document
|
||||
*/
|
||||
async existsByCartId(cartId) {
|
||||
const order = await this.retrieveByCartId(cartId).catch(_ => undefined)
|
||||
const order = await this.retrieveByCartId(cartId).catch((_) => undefined)
|
||||
if (!order) {
|
||||
return false
|
||||
}
|
||||
@@ -409,7 +408,7 @@ class OrderService extends BaseService {
|
||||
* @return {Promise} the result of the find operation
|
||||
*/
|
||||
async completeOrder(orderId) {
|
||||
return this.atomicPhase_(async manager => {
|
||||
return this.atomicPhase_(async (manager) => {
|
||||
const order = await this.retrieve(orderId)
|
||||
|
||||
if (order.status === "canceled") {
|
||||
@@ -428,7 +427,7 @@ class OrderService extends BaseService {
|
||||
}
|
||||
)
|
||||
|
||||
await completeOrderJob.finished().catch(error => {
|
||||
await completeOrderJob.finished().catch((error) => {
|
||||
throw error
|
||||
})
|
||||
|
||||
@@ -445,7 +444,7 @@ class OrderService extends BaseService {
|
||||
* @return {Promise} resolves to the creation result.
|
||||
*/
|
||||
async createFromCart(cartId) {
|
||||
return this.atomicPhase_(async manager => {
|
||||
return this.atomicPhase_(async (manager) => {
|
||||
const cart = await this.cartService_
|
||||
.withTransaction(manager)
|
||||
.retrieve(cartId, {
|
||||
@@ -619,6 +618,7 @@ class OrderService extends BaseService {
|
||||
* @param {string} fulfillmentId - the fulfillment that has now been shipped
|
||||
* @param {TrackingLink[]} trackingLinks - array of tracking numebers
|
||||
* associated with the shipment
|
||||
* @param {Object} config - the config of the order that has been shipped
|
||||
* @param {Dictionary<String, String>} metadata - optional metadata to add to
|
||||
* the fulfillment
|
||||
* @return {order} the resulting order following the update.
|
||||
@@ -634,7 +634,7 @@ class OrderService extends BaseService {
|
||||
) {
|
||||
const { metadata, no_notification } = config
|
||||
|
||||
return this.atomicPhase_(async manager => {
|
||||
return this.atomicPhase_(async (manager) => {
|
||||
const order = await this.retrieve(orderId, { relations: ["items"] })
|
||||
const shipment = await this.fulfillmentService_.retrieve(fulfillmentId)
|
||||
|
||||
@@ -666,7 +666,7 @@ class OrderService extends BaseService {
|
||||
|
||||
order.fulfillment_status = "shipped"
|
||||
for (const item of order.items) {
|
||||
const shipped = shipmentRes.items.find(si => si.item_id === item.id)
|
||||
const shipped = shipmentRes.items.find((si) => si.item_id === item.id)
|
||||
if (shipped) {
|
||||
const shippedQty = (item.shipped_quantity || 0) + shipped.quantity
|
||||
if (shippedQty !== item.quantity) {
|
||||
@@ -700,11 +700,11 @@ class OrderService extends BaseService {
|
||||
|
||||
/**
|
||||
* Creates an order
|
||||
* @param {object} order - the order to create
|
||||
* @param {object} data - the data to create an order
|
||||
* @return {Promise} resolves to the creation result.
|
||||
*/
|
||||
async create(data) {
|
||||
return this.atomicPhase_(async manager => {
|
||||
return this.atomicPhase_(async (manager) => {
|
||||
const orderRepo = manager.getCustomRepository(this.orderRepository_)
|
||||
const order = orderRepo.create(data)
|
||||
const result = await orderRepo.save(order)
|
||||
@@ -720,7 +720,7 @@ class OrderService extends BaseService {
|
||||
|
||||
/**
|
||||
* Updates the order's billing address.
|
||||
* @param {string} orderId - the id of the order to update
|
||||
* @param {object} order - the order to update
|
||||
* @param {object} address - the value to set the billing address to
|
||||
* @return {Promise} the result of the update operation
|
||||
*/
|
||||
@@ -755,7 +755,7 @@ class OrderService extends BaseService {
|
||||
|
||||
/**
|
||||
* Updates the order's shipping address.
|
||||
* @param {string} orderId - the id of the order to update
|
||||
* @param {object} order - the order to update
|
||||
* @param {object} address - the value to set the shipping address to
|
||||
* @return {Promise} the result of the update operation
|
||||
*/
|
||||
@@ -787,7 +787,7 @@ class OrderService extends BaseService {
|
||||
}
|
||||
|
||||
async addShippingMethod(orderId, optionId, data, config = {}) {
|
||||
return this.atomicPhase_(async manager => {
|
||||
return this.atomicPhase_(async (manager) => {
|
||||
const order = await this.retrieve(orderId, {
|
||||
select: ["subtotal"],
|
||||
relations: [
|
||||
@@ -845,7 +845,7 @@ class OrderService extends BaseService {
|
||||
* @return {Promise} resolves to the update result.
|
||||
*/
|
||||
async update(orderId, update) {
|
||||
return this.atomicPhase_(async manager => {
|
||||
return this.atomicPhase_(async (manager) => {
|
||||
const order = await this.retrieve(orderId)
|
||||
|
||||
if (order.status === "canceled") {
|
||||
@@ -874,13 +874,7 @@ class OrderService extends BaseService {
|
||||
)
|
||||
}
|
||||
|
||||
const {
|
||||
metadata,
|
||||
items,
|
||||
billing_address,
|
||||
shipping_address,
|
||||
...rest
|
||||
} = update
|
||||
const { ...rest } = update
|
||||
|
||||
if ("metadata" in update) {
|
||||
order.metadata = this.setMetadata_(order, update.metadata)
|
||||
@@ -932,7 +926,7 @@ class OrderService extends BaseService {
|
||||
* @return {Promise} result of the update operation.
|
||||
*/
|
||||
async cancel(orderId) {
|
||||
return this.atomicPhase_(async manager => {
|
||||
return this.atomicPhase_(async (manager) => {
|
||||
const order = await this.retrieve(orderId, {
|
||||
relations: [
|
||||
"fulfillments",
|
||||
@@ -952,16 +946,16 @@ class OrderService extends BaseService {
|
||||
}
|
||||
|
||||
const throwErrorIf = (arr, pred, type) =>
|
||||
arr?.filter(pred).find(_ => {
|
||||
arr?.filter(pred).find((_) => {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_ALLOWED,
|
||||
`All ${type} must be canceled before canceling an order`
|
||||
)
|
||||
})
|
||||
const notCanceled = o => !o.canceled_at
|
||||
const notCanceled = (o) => !o.canceled_at
|
||||
|
||||
throwErrorIf(order.fulfillments, notCanceled, "fulfillments")
|
||||
throwErrorIf(order.returns, r => r.status !== "canceled", "returns")
|
||||
throwErrorIf(order.returns, (r) => r.status !== "canceled", "returns")
|
||||
throwErrorIf(order.swaps, notCanceled, "swaps")
|
||||
throwErrorIf(order.claims, notCanceled, "claims")
|
||||
|
||||
@@ -1000,7 +994,7 @@ class OrderService extends BaseService {
|
||||
* @return {Promise} result of the update operation.
|
||||
*/
|
||||
async capturePayment(orderId) {
|
||||
return this.atomicPhase_(async manager => {
|
||||
return this.atomicPhase_(async (manager) => {
|
||||
const orderRepo = manager.getCustomRepository(this.orderRepository_)
|
||||
const order = await this.retrieve(orderId, { relations: ["payments"] })
|
||||
|
||||
@@ -1017,7 +1011,7 @@ class OrderService extends BaseService {
|
||||
const result = await this.paymentProviderService_
|
||||
.withTransaction(manager)
|
||||
.capturePayment(p)
|
||||
.catch(err => {
|
||||
.catch((err) => {
|
||||
this.eventBus_
|
||||
.withTransaction(manager)
|
||||
.emit(OrderService.Events.PAYMENT_CAPTURE_FAILED, {
|
||||
@@ -1039,7 +1033,7 @@ class OrderService extends BaseService {
|
||||
}
|
||||
|
||||
order.payments = payments
|
||||
order.payment_status = payments.every(p => p.captured_at !== null)
|
||||
order.payment_status = payments.every((p) => p.captured_at !== null)
|
||||
? "captured"
|
||||
: "requires_action"
|
||||
|
||||
@@ -1095,6 +1089,8 @@ class OrderService extends BaseService {
|
||||
* we need to partition the order items, such that they can be sent
|
||||
* to their respective fulfillment provider.
|
||||
* @param {string} orderId - id of order to cancel.
|
||||
* @param {Object} itemsToFulfill - items to fulfil.
|
||||
* @param {Object} config - the config to cancel.
|
||||
* @return {Promise} result of the update operation.
|
||||
*/
|
||||
async createFulfillment(
|
||||
@@ -1107,7 +1103,7 @@ class OrderService extends BaseService {
|
||||
) {
|
||||
const { metadata, no_notification } = config
|
||||
|
||||
return this.atomicPhase_(async manager => {
|
||||
return this.atomicPhase_(async (manager) => {
|
||||
// NOTE: we are telling the service to calculate all totals for us which
|
||||
// will add to what is fetched from the database. We want this to happen
|
||||
// so that we get all order details. These will thereafter be forwarded
|
||||
@@ -1169,7 +1165,7 @@ class OrderService extends BaseService {
|
||||
// Update all line items to reflect fulfillment
|
||||
for (const item of order.items) {
|
||||
const fulfillmentItem = successfullyFulfilled.find(
|
||||
f => item.id === f.item_id
|
||||
(f) => item.id === f.item_id
|
||||
)
|
||||
|
||||
if (fulfillmentItem) {
|
||||
@@ -1217,10 +1213,10 @@ class OrderService extends BaseService {
|
||||
/**
|
||||
* Cancels a fulfillment (if related to an order)
|
||||
* @param {string} fulfillmentId - the ID of the fulfillment to cancel
|
||||
* @returns updated order
|
||||
* @return {Promise} updated order
|
||||
*/
|
||||
async cancelFulfillment(fulfillmentId) {
|
||||
return this.atomicPhase_(async manager => {
|
||||
return this.atomicPhase_(async (manager) => {
|
||||
const canceled = await this.fulfillmentService_
|
||||
.withTransaction(manager)
|
||||
.cancelFulfillment(fulfillmentId)
|
||||
@@ -1264,12 +1260,12 @@ class OrderService extends BaseService {
|
||||
async getFulfillmentItems_(order, items, transformer) {
|
||||
const toReturn = await Promise.all(
|
||||
items.map(async ({ item_id, quantity }) => {
|
||||
const item = order.items.find(i => i.id.equals(item_id))
|
||||
const item = order.items.find((i) => i.id.equals(item_id))
|
||||
return transformer(item, quantity)
|
||||
})
|
||||
)
|
||||
|
||||
return toReturn.filter(i => !!i)
|
||||
return toReturn.filter((i) => !!i)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1279,7 +1275,7 @@ class OrderService extends BaseService {
|
||||
* @return {Promise} the result of the update operation
|
||||
*/
|
||||
async archive(orderId) {
|
||||
return this.atomicPhase_(async manager => {
|
||||
return this.atomicPhase_(async (manager) => {
|
||||
const order = await this.retrieve(orderId)
|
||||
|
||||
if (order.status !== ("completed" || "refunded")) {
|
||||
@@ -1298,6 +1294,12 @@ class OrderService extends BaseService {
|
||||
|
||||
/**
|
||||
* Refunds a given amount back to the customer.
|
||||
* @param {string} orderId - id of the order to refund.
|
||||
* @param {float} refundAmount - the amount to refund.
|
||||
* @param {string} reason - the reason to refund.
|
||||
* @param {string} note - note for refund.
|
||||
* @param {Object} config - the config for refund.
|
||||
* @return {Promise} the result of the refund operation.
|
||||
*/
|
||||
async createRefund(
|
||||
orderId,
|
||||
@@ -1310,7 +1312,7 @@ class OrderService extends BaseService {
|
||||
) {
|
||||
const { no_notification } = config
|
||||
|
||||
return this.atomicPhase_(async manager => {
|
||||
return this.atomicPhase_(async (manager) => {
|
||||
const order = await this.retrieve(orderId, {
|
||||
select: ["refundable_amount", "total", "refunded_total"],
|
||||
relations: ["payments"],
|
||||
@@ -1380,7 +1382,7 @@ class OrderService extends BaseService {
|
||||
}
|
||||
|
||||
if (totalsFields.includes("items.refundable")) {
|
||||
order.items = order.items.map(i => ({
|
||||
order.items = order.items.map((i) => ({
|
||||
...i,
|
||||
refundable: this.totalsService_.getLineItemRefund(order, {
|
||||
...i,
|
||||
@@ -1395,7 +1397,7 @@ class OrderService extends BaseService {
|
||||
order.swaps.length
|
||||
) {
|
||||
for (const s of order.swaps) {
|
||||
s.additional_items = s.additional_items.map(i => ({
|
||||
s.additional_items = s.additional_items.map((i) => ({
|
||||
...i,
|
||||
refundable: this.totalsService_.getLineItemRefund(order, {
|
||||
...i,
|
||||
@@ -1418,10 +1420,11 @@ class OrderService extends BaseService {
|
||||
* mismatches.
|
||||
* @param {string} orderId - the order to return.
|
||||
* @param {object} receivedReturn - the received return
|
||||
* @param {float} customRefundAmount - the custom refund amount return
|
||||
* @return {Promise} the result of the update operation
|
||||
*/
|
||||
async registerReturnReceived(orderId, receivedReturn, customRefundAmount) {
|
||||
return this.atomicPhase_(async manager => {
|
||||
return this.atomicPhase_(async (manager) => {
|
||||
const order = await this.retrieve(orderId, {
|
||||
select: ["total", "refunded_total", "refundable_amount"],
|
||||
relations: ["items", "returns", "payments"],
|
||||
@@ -1441,7 +1444,7 @@ class OrderService extends BaseService {
|
||||
)
|
||||
}
|
||||
|
||||
let refundAmount = customRefundAmount || receivedReturn.refund_amount
|
||||
const refundAmount = customRefundAmount || receivedReturn.refund_amount
|
||||
|
||||
const orderRepo = manager.getCustomRepository(this.orderRepository_)
|
||||
|
||||
@@ -1510,7 +1513,7 @@ class OrderService extends BaseService {
|
||||
const keyPath = `metadata.${key}`
|
||||
return this.orderModel_
|
||||
.updateOne({ _id: validatedId }, { $unset: { [keyPath]: "" } })
|
||||
.catch(err => {
|
||||
.catch((err) => {
|
||||
throw new MedusaError(MedusaError.Types.DB_ERROR, err.message)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
import _ from "lodash"
|
||||
import { Validator, MedusaError } from "medusa-core-utils"
|
||||
import { MedusaError } from "medusa-core-utils"
|
||||
import { BaseService } from "medusa-interfaces"
|
||||
import { countries } from "../utils/countries"
|
||||
|
||||
/**
|
||||
* Provides layer to manipulate regions.
|
||||
* @implements BaseService
|
||||
* @extends BaseService
|
||||
*/
|
||||
class RegionService extends BaseService {
|
||||
static Events = {
|
||||
@@ -83,11 +82,11 @@ class RegionService extends BaseService {
|
||||
|
||||
/**
|
||||
* Creates a region.
|
||||
* @param {Region} rawRegion - the unvalidated region
|
||||
* @param {Region} regionObject - the unvalidated region
|
||||
* @return {Region} the newly created region
|
||||
*/
|
||||
async create(regionObject) {
|
||||
return this.atomicPhase_(async manager => {
|
||||
return this.atomicPhase_(async (manager) => {
|
||||
const regionRepository = manager.getCustomRepository(
|
||||
this.regionRepository_
|
||||
)
|
||||
@@ -118,7 +117,7 @@ class RegionService extends BaseService {
|
||||
}
|
||||
|
||||
if (metadata) {
|
||||
regionObject.metadata = this.setMetadata_(region, metadata)
|
||||
regionObject.metadata = this.setMetadata_(regionObject, metadata)
|
||||
}
|
||||
|
||||
for (const [key, value] of Object.entries(validated)) {
|
||||
@@ -145,7 +144,7 @@ class RegionService extends BaseService {
|
||||
* @return {Promise} the result of the update operation
|
||||
*/
|
||||
async update(regionId, update) {
|
||||
return this.atomicPhase_(async manager => {
|
||||
return this.atomicPhase_(async (manager) => {
|
||||
const regionRepository = manager.getCustomRepository(
|
||||
this.regionRepository_
|
||||
)
|
||||
@@ -218,17 +217,17 @@ class RegionService extends BaseService {
|
||||
|
||||
if (region.countries) {
|
||||
region.countries = await Promise.all(
|
||||
region.countries.map(countryCode =>
|
||||
region.countries.map((countryCode) =>
|
||||
this.validateCountry_(countryCode, id)
|
||||
)
|
||||
).catch(err => {
|
||||
).catch((err) => {
|
||||
throw err
|
||||
})
|
||||
}
|
||||
|
||||
if (region.payment_providers) {
|
||||
region.payment_providers = await Promise.all(
|
||||
region.payment_providers.map(async pId => {
|
||||
region.payment_providers.map(async (pId) => {
|
||||
const pp = await ppRepository.findOne({ where: { id: pId } })
|
||||
if (!pp) {
|
||||
throw new MedusaError(
|
||||
@@ -244,7 +243,7 @@ class RegionService extends BaseService {
|
||||
|
||||
if (region.fulfillment_providers) {
|
||||
region.fulfillment_providers = await Promise.all(
|
||||
region.fulfillment_providers.map(async fId => {
|
||||
region.fulfillment_providers.map(async (fId) => {
|
||||
const fp = await fpRepository.findOne({ where: { id: fId } })
|
||||
if (!fp) {
|
||||
throw new MedusaError(
|
||||
@@ -283,7 +282,7 @@ class RegionService extends BaseService {
|
||||
.withTransaction(this.transactionManager_)
|
||||
.retrieve(["currencies"])
|
||||
|
||||
const storeCurrencies = store.currencies.map(curr => curr.code)
|
||||
const storeCurrencies = store.currencies.map((curr) => curr.code)
|
||||
|
||||
if (!storeCurrencies.includes(currencyCode.toLowerCase())) {
|
||||
throw new MedusaError(
|
||||
@@ -305,7 +304,7 @@ class RegionService extends BaseService {
|
||||
)
|
||||
|
||||
const countryCode = code.toUpperCase()
|
||||
const validCountry = countries.find(c => c.alpha2 === countryCode)
|
||||
const validCountry = countries.find((c) => c.alpha2 === countryCode)
|
||||
if (!validCountry) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.INVALID_DATA,
|
||||
@@ -339,6 +338,7 @@ class RegionService extends BaseService {
|
||||
/**
|
||||
* Retrieves a region by its id.
|
||||
* @param {string} regionId - the id of the region to retrieve
|
||||
* @param {object} config - configuration settings
|
||||
* @return {Region} the region
|
||||
*/
|
||||
async retrieve(regionId, config = {}) {
|
||||
@@ -361,7 +361,8 @@ class RegionService extends BaseService {
|
||||
|
||||
/**
|
||||
* Lists all regions based on a query
|
||||
* @param {object} listOptions - query object for find
|
||||
* @param {object} selector - query object for find
|
||||
* @param {object} config - configuration settings
|
||||
* @return {Promise} result of the find operation
|
||||
*/
|
||||
async list(selector = {}, config = { relations: [], skip: 0, take: 10 }) {
|
||||
@@ -377,12 +378,14 @@ class RegionService extends BaseService {
|
||||
* @return {Promise} the result of the delete operation
|
||||
*/
|
||||
async delete(regionId) {
|
||||
return this.atomicPhase_(async manager => {
|
||||
return this.atomicPhase_(async (manager) => {
|
||||
const regionRepo = manager.getCustomRepository(this.regionRepository_)
|
||||
|
||||
const region = await regionRepo.findOne({ where: { id: regionId } })
|
||||
|
||||
if (!region) return Promise.resolve()
|
||||
if (!region) {
|
||||
return Promise.resolve()
|
||||
}
|
||||
|
||||
await regionRepo.softRemove(region)
|
||||
|
||||
@@ -397,7 +400,7 @@ class RegionService extends BaseService {
|
||||
* @return {Promise} the result of the update operation
|
||||
*/
|
||||
async addCountry(regionId, code) {
|
||||
return this.atomicPhase_(async manager => {
|
||||
return this.atomicPhase_(async (manager) => {
|
||||
const regionRepo = manager.getCustomRepository(this.regionRepository_)
|
||||
|
||||
const country = await this.validateCountry_(code, regionId)
|
||||
@@ -407,7 +410,7 @@ class RegionService extends BaseService {
|
||||
// Check if region already has country
|
||||
if (
|
||||
region.countries &&
|
||||
region.countries.map(c => c.iso_2).includes(country.iso_2)
|
||||
region.countries.map((c) => c.iso_2).includes(country.iso_2)
|
||||
) {
|
||||
return region
|
||||
}
|
||||
@@ -434,7 +437,7 @@ class RegionService extends BaseService {
|
||||
* @return {Promise} the result of the update operation
|
||||
*/
|
||||
async removeCountry(regionId, code) {
|
||||
return this.atomicPhase_(async manager => {
|
||||
return this.atomicPhase_(async (manager) => {
|
||||
const regionRepo = manager.getCustomRepository(this.regionRepository_)
|
||||
|
||||
const region = await this.retrieve(regionId, { relations: ["countries"] })
|
||||
@@ -442,13 +445,13 @@ class RegionService extends BaseService {
|
||||
// Check if region contains country. If not, we simpy resolve
|
||||
if (
|
||||
region.countries &&
|
||||
!region.countries.map(c => c.iso_2).includes(code)
|
||||
!region.countries.map((c) => c.iso_2).includes(code)
|
||||
) {
|
||||
return region
|
||||
}
|
||||
|
||||
region.countries = region.countries.filter(
|
||||
country => country.iso_2 !== code
|
||||
(country) => country.iso_2 !== code
|
||||
)
|
||||
|
||||
const updated = await regionRepo.save(region)
|
||||
@@ -470,7 +473,7 @@ class RegionService extends BaseService {
|
||||
* @return {Promise} the result of the update operation
|
||||
*/
|
||||
async addPaymentProvider(regionId, providerId) {
|
||||
return this.atomicPhase_(async manager => {
|
||||
return this.atomicPhase_(async (manager) => {
|
||||
const regionRepo = manager.getCustomRepository(this.regionRepository_)
|
||||
const ppRepo = manager.getCustomRepository(
|
||||
this.paymentProviderRepository_
|
||||
@@ -517,7 +520,7 @@ class RegionService extends BaseService {
|
||||
* @return {Promise} the result of the update operation
|
||||
*/
|
||||
async addFulfillmentProvider(regionId, providerId) {
|
||||
return this.atomicPhase_(async manager => {
|
||||
return this.atomicPhase_(async (manager) => {
|
||||
const regionRepo = manager.getCustomRepository(this.regionRepository_)
|
||||
const fpRepo = manager.getCustomRepository(
|
||||
this.fulfillmentProviderRepository_
|
||||
@@ -561,7 +564,7 @@ class RegionService extends BaseService {
|
||||
* @return {Promise} the result of the update operation
|
||||
*/
|
||||
async removePaymentProvider(regionId, providerId) {
|
||||
return this.atomicPhase_(async manager => {
|
||||
return this.atomicPhase_(async (manager) => {
|
||||
const regionRepo = manager.getCustomRepository(this.regionRepository_)
|
||||
|
||||
const region = await this.retrieve(regionId, {
|
||||
@@ -595,7 +598,7 @@ class RegionService extends BaseService {
|
||||
* @return {Promise} the result of the update operation
|
||||
*/
|
||||
async removeFulfillmentProvider(regionId, providerId) {
|
||||
return this.atomicPhase_(async manager => {
|
||||
return this.atomicPhase_(async (manager) => {
|
||||
const regionRepo = manager.getCustomRepository(this.regionRepository_)
|
||||
|
||||
const region = await this.retrieve(regionId, {
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
import { BaseService } from "medusa-interfaces"
|
||||
import mongoose from "mongoose"
|
||||
import _ from "lodash"
|
||||
|
||||
class TransactionService extends BaseService {
|
||||
constructor({}) {
|
||||
constructor() {
|
||||
super()
|
||||
}
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@ import { BaseService } from "medusa-interfaces"
|
||||
|
||||
/**
|
||||
* Provides layer to manipulate users.
|
||||
* @implements BaseService
|
||||
* @extends BaseService
|
||||
*/
|
||||
class UserService extends BaseService {
|
||||
static Events = {
|
||||
@@ -48,9 +48,7 @@ class UserService extends BaseService {
|
||||
* @return {string} the validated email
|
||||
*/
|
||||
validateEmail_(email) {
|
||||
const schema = Validator.string()
|
||||
.email()
|
||||
.required()
|
||||
const schema = Validator.string().email().required()
|
||||
const { value, error } = schema.validate(email)
|
||||
if (error) {
|
||||
throw new MedusaError(
|
||||
@@ -75,6 +73,7 @@ class UserService extends BaseService {
|
||||
* Gets a user by id.
|
||||
* Throws in case of DB Error and if user was not found.
|
||||
* @param {string} userId - the id of the user to get.
|
||||
* @param {Object} config - query configs
|
||||
* @return {Promise<User>} the user document.
|
||||
*/
|
||||
async retrieve(userId, config = {}) {
|
||||
@@ -99,6 +98,7 @@ class UserService extends BaseService {
|
||||
* Gets a user by api token.
|
||||
* Throws in case of DB Error and if user was not found.
|
||||
* @param {string} apiToken - the token of the user to get.
|
||||
* @param {string[]} relations - relations to include with the user
|
||||
* @return {Promise<User>} the user document.
|
||||
*/
|
||||
async retrieveByApiToken(apiToken, relations = []) {
|
||||
@@ -123,6 +123,7 @@ class UserService extends BaseService {
|
||||
* Gets a user by email.
|
||||
* Throws in case of DB Error and if user was not found.
|
||||
* @param {string} email - the email of the user to get.
|
||||
* @param {Object} config - query config
|
||||
* @return {Promise<User>} the user document.
|
||||
*/
|
||||
async retrieveByEmail(email, config = {}) {
|
||||
@@ -144,7 +145,7 @@ class UserService extends BaseService {
|
||||
/**
|
||||
* Hashes a password
|
||||
* @param {string} password - the value to hash
|
||||
* @return hashed password
|
||||
* @return {string} hashed password
|
||||
*/
|
||||
async hashPassword_(password) {
|
||||
const buf = await Scrypt.kdf(password, { logN: 1, r: 1, p: 1 })
|
||||
@@ -155,10 +156,11 @@ class UserService extends BaseService {
|
||||
* Creates a user with username being validated.
|
||||
* Fails if email is not a valid format.
|
||||
* @param {object} user - the user to create
|
||||
* @param {string} password - user's password to hash
|
||||
* @return {Promise} the result of create
|
||||
*/
|
||||
async create(user, password) {
|
||||
return this.atomicPhase_(async manager => {
|
||||
return this.atomicPhase_(async (manager) => {
|
||||
const userRepo = manager.getCustomRepository(this.userRepository_)
|
||||
|
||||
const validatedEmail = this.validateEmail_(user.email)
|
||||
@@ -177,11 +179,12 @@ class UserService extends BaseService {
|
||||
|
||||
/**
|
||||
* Updates a user.
|
||||
* @param {object} user - the user to create
|
||||
* @param {object} userId - id of the user to update
|
||||
* @param {object} update - the values to be updated on the user
|
||||
* @return {Promise} the result of create
|
||||
*/
|
||||
async update(userId, update) {
|
||||
return this.atomicPhase_(async manager => {
|
||||
return this.atomicPhase_(async (manager) => {
|
||||
const userRepo = manager.getCustomRepository(this.userRepository_)
|
||||
const validatedId = this.validateId_(userId)
|
||||
|
||||
@@ -222,13 +225,15 @@ class UserService extends BaseService {
|
||||
* @return {Promise} the result of the delete operation.
|
||||
*/
|
||||
async delete(userId) {
|
||||
return this.atomicPhase_(async manager => {
|
||||
return this.atomicPhase_(async (manager) => {
|
||||
const userRepo = manager.getCustomRepository(this.userRepository_)
|
||||
|
||||
// Should not fail, if user does not exist, since delete is idempotent
|
||||
const user = await userRepo.findOne({ where: { id: userId } })
|
||||
|
||||
if (!user) return Promise.resolve()
|
||||
if (!user) {
|
||||
return Promise.resolve()
|
||||
}
|
||||
|
||||
await userRepo.softRemove(user)
|
||||
|
||||
@@ -242,10 +247,10 @@ class UserService extends BaseService {
|
||||
* password does not work.
|
||||
* @param {string} userId - the userId to set password for
|
||||
* @param {string} password - the old password to set
|
||||
* @returns {Promise} the result of the update operation
|
||||
* @return {Promise} the result of the update operation
|
||||
*/
|
||||
async setPassword_(userId, password) {
|
||||
return this.atomicPhase_(async manager => {
|
||||
return this.atomicPhase_(async (manager) => {
|
||||
const userRepo = manager.getCustomRepository(this.userRepository_)
|
||||
|
||||
const user = await this.retrieve(userId)
|
||||
@@ -270,8 +275,8 @@ class UserService extends BaseService {
|
||||
* The token will be signed with the users current password hash as a secret
|
||||
* a long side a payload with userId and the expiry time for the token, which
|
||||
* is always 15 minutes.
|
||||
* @param {User} user - the user to reset password for
|
||||
* @returns {string} the generated JSON web token
|
||||
* @param {string} userId - the id of the user to reset password for
|
||||
* @return {string} the generated JSON web token
|
||||
*/
|
||||
async generateResetPasswordToken(userId) {
|
||||
const user = await this.retrieve(userId)
|
||||
|
||||
Reference in new issue
Block a user