PR Review fixes

This commit is contained in:
olivermrbl
2021-06-07 15:23:41 +02:00
parent e79b8f6494
commit c0f2d629ab
18 changed files with 195 additions and 91 deletions
+22 -12
View File
@@ -2,19 +2,29 @@ import Joi from "joi"
Joi.objectId = require("joi-objectid")(Joi)
// if address is a string, we assume that it is an id
Joi.address = () => {
return Joi.object().keys({
first_name: Joi.string().required(),
last_name: Joi.string().required(),
address_1: Joi.string().required(),
address_2: Joi.string().allow(null),
city: Joi.string().required(),
country_code: Joi.string().required(),
province: Joi.string().allow(null),
postal_code: Joi.string().required(),
phone: Joi.string().optional(),
metadata: Joi.object().allow(null),
})
return Joi.alternatives().try(
Joi.string(),
Joi.object().keys({
first_name: Joi.string().required(),
last_name: Joi.string().required(),
address_1: Joi.string().required(),
address_2: Joi.string()
.allow(null, "")
.optional(),
city: Joi.string().required(),
country_code: Joi.string().required(),
province: Joi.string()
.allow(null, "")
.optional(),
postal_code: Joi.string().required(),
phone: Joi.string().optional(),
metadata: Joi.object()
.allow(null, {})
.optional(),
})
)
}
Joi.dateFilter = () => {
@@ -25,12 +25,6 @@ import { defaultFields, defaultRelations } from "."
* description: "The Address to be used for shipping."
* anyOf:
* - $ref: "#/components/schemas/address"
* billing_address_id:
* description: The id of an existing billing Address
* type: string
* shipping_address_id:
* description: The id of an existing shipping Address
* type: string
* items:
* description: The Line Items that have been received.
* type: array
@@ -98,15 +92,13 @@ import { defaultFields, defaultRelations } from "."
export default async (req, res) => {
const schema = Validator.object().keys({
status: Validator.string()
.valid("open", "awaiting", "completed")
.valid("open", "completed")
.optional(),
email: Validator.string()
.email()
.required(),
billing_address: Validator.address().optional(),
shipping_address: Validator.address().optional(),
billing_address_id: Validator.string().optional(),
shipping_address_id: Validator.string().optional(),
items: Validator.array()
.items({
variant_id: Validator.string()
@@ -66,10 +66,7 @@ export default async (req, res) => {
.withTransaction(manager)
.retrieve(id, { select: defaultFields, relations: ["cart"] })
if (
draftOrder.status === "completed" ||
draftOrder.status === "awaiting"
) {
if (draftOrder.status === "completed") {
throw new MedusaError(
MedusaError.Types.NOT_ALLOWED,
"You are only allowed to update open draft orders"
@@ -35,10 +35,7 @@ export default async (req, res) => {
.withTransaction(manager)
.retrieve(id, { select: defaultFields })
if (
draftOrder.status === "completed" ||
draftOrder.status === "awaiting"
) {
if (draftOrder.status === "completed") {
throw new MedusaError(
MedusaError.Types.NOT_ALLOWED,
"You are only allowed to update open draft orders"
@@ -67,11 +67,6 @@ export default async (req, res) => {
code: Validator.string(),
})
.optional(),
gift_cards: Validator.array()
.items({
code: Validator.string(),
})
.optional(),
customer_id: Validator.string().optional(),
})
@@ -86,7 +81,7 @@ export default async (req, res) => {
const draftOrder = await draftOrderService.retrieve(id)
if (draftOrder.status === "completed" || draftOrder.status === "awaiting") {
if (draftOrder.status === "completed") {
throw new MedusaError(
MedusaError.Types.NOT_ALLOWED,
"You are only allowed to update open draft orders"
@@ -64,10 +64,7 @@ export default async (req, res) => {
relations: ["cart", "cart.items"],
})
if (
draftOrder.status === "completed" ||
draftOrder.status === "awaiting"
) {
if (draftOrder.status === "completed") {
throw new MedusaError(
MedusaError.Types.NOT_ALLOWED,
"You are only allowed to update open draft orders"
@@ -75,8 +75,8 @@ export default async (req, res) => {
email: Validator.string()
.email()
.optional(),
billing_address: Validator.object().optional(),
shipping_address: Validator.object().optional(),
billing_address: Validator.address().optional(),
shipping_address: Validator.address().optional(),
gift_cards: Validator.array()
.items({
code: Validator.string(),
@@ -0,0 +1,28 @@
import {MigrationInterface, QueryRunner} from "typeorm";
export class draftOrderCompletedAt1623063141210 implements MigrationInterface {
name = 'draftOrderCompletedAt1623063141210'
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "draft_order" ADD "completed_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now()`);
await queryRunner.query(`ALTER TYPE "public"."draft_order_status_enum" RENAME TO "draft_order_status_enum_old"`);
await queryRunner.query(`CREATE TYPE "draft_order_status_enum" AS ENUM('open', 'completed')`);
await queryRunner.query(`ALTER TABLE "draft_order" ALTER COLUMN "status" DROP DEFAULT`);
await queryRunner.query(`ALTER TABLE "draft_order" ALTER COLUMN "status" TYPE "draft_order_status_enum" USING "status"::"text"::"draft_order_status_enum"`);
await queryRunner.query(`ALTER TABLE "draft_order" ALTER COLUMN "status" SET DEFAULT 'open'`);
await queryRunner.query(`DROP TYPE "draft_order_status_enum_old"`);
await queryRunner.query(`COMMENT ON COLUMN "draft_order"."status" IS NULL`);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`COMMENT ON COLUMN "draft_order"."status" IS NULL`);
await queryRunner.query(`CREATE TYPE "draft_order_status_enum_old" AS ENUM('open', 'awaiting', 'completed')`);
await queryRunner.query(`ALTER TABLE "draft_order" ALTER COLUMN "status" DROP DEFAULT`);
await queryRunner.query(`ALTER TABLE "draft_order" ALTER COLUMN "status" TYPE "draft_order_status_enum_old" USING "status"::"text"::"draft_order_status_enum_old"`);
await queryRunner.query(`ALTER TABLE "draft_order" ALTER COLUMN "status" SET DEFAULT 'open'`);
await queryRunner.query(`DROP TYPE "draft_order_status_enum"`);
await queryRunner.query(`ALTER TYPE "draft_order_status_enum_old" RENAME TO "draft_order_status_enum"`);
await queryRunner.query(`ALTER TABLE "draft_order" DROP COLUMN "completed_at"`);
}
}
+6 -2
View File
@@ -17,7 +17,6 @@ import { Order } from "./order"
enum DraftOrderStatus {
OPEN = "open",
AWAITING = "awaiting",
COMPLETED = "completed",
}
@@ -59,6 +58,9 @@ export class DraftOrder {
@UpdateDateColumn({ type: "timestamptz" })
updated_at: Date
@UpdateDateColumn({ type: "timestamptz" })
completed_at: Date
@Column({ type: "jsonb", nullable: true })
metadata: any
@@ -85,7 +87,6 @@ export class DraftOrder {
* type: string
* enum:
* - open
* - awaiting
* - completed
* display_id:
* type: string
@@ -111,6 +112,9 @@ export class DraftOrder {
* deleted_at:
* type: string
* format: date-time
* completed_at:
* type: string
* format: date-time
* metadata:
* type: object
* idempotency_key:
+59 -40
View File
@@ -306,7 +306,12 @@ class CartService extends BaseService {
const regCountries = region.countries.map(({ iso_2 }) => iso_2)
if (!data.shipping_address && !data.shipping_address_id) {
if (data.shipping_address && typeof data.shipping_address === `string`) {
const addr = await addressRepo.findOne(data.shipping_address)
data.shipping_address = addr
}
if (!data.shipping_address) {
if (region.countries.length === 1) {
// Preselect the country if the region only has 1
// and create address entity
@@ -315,22 +320,11 @@ class CartService extends BaseService {
})
}
} else {
if (data.shipping_address) {
if (!regCountries.includes(data.shipping_address.country_code)) {
throw new MedusaError(
MedusaError.Types.NOT_ALLOWED,
"Shipping country not in region"
)
}
}
if (data.shipping_address_id) {
const addr = await addressRepo.findOne(data.shipping_address_id)
if (!regCountries.includes(addr.country_code)) {
throw new MedusaError(
MedusaError.Types.NOT_ALLOWED,
"Shipping country not in region"
)
}
if (!regCountries.includes(data.shipping_address.country_code)) {
throw new MedusaError(
MedusaError.Types.NOT_ALLOWED,
"Shipping country not in region"
)
}
}
@@ -782,20 +776,32 @@ class CartService extends BaseService {
* @param {object} address - the value to set the billing address to
* @return {Promise} the result of the update operation
*/
async updateBillingAddress_(cart, address, addrRepo) {
address.country_code = address.country_code.toLowerCase()
if (cart.billing_address_id) {
const addr = await addrRepo.findOne({
where: { id: cart.billing_address_id },
async updateBillingAddress_(cart, addressOrId, addrRepo) {
if (typeof addressOrId === `string`) {
addressOrId = await addrRepo.findOne({
where: { id: addressOrId },
})
}
await addrRepo.save({ ...addr, ...address })
addressOrId.country_code = addressOrId.country_code.toLowerCase()
if (addressOrId.id) {
cart.billing_address_id = addressOrId.id
cart.billing_address = addressOrId
} else {
const created = addrRepo.create({
...address,
})
if (cart.billing_address_id) {
const addr = await addrRepo.findOne({
where: { id: cart.billing_address_id },
})
cart.billing_address = created
await addrRepo.save({ ...addr, ...addressOrId })
} else {
const created = addrRepo.create({
...addressOrId,
})
cart.billing_address = created
}
}
}
@@ -805,11 +811,19 @@ class CartService extends BaseService {
* @param {object} address - the value to set the shipping address to
* @return {Promise} the result of the update operation
*/
async updateShippingAddress_(cart, address, addrRepo) {
address.country_code = address.country_code.toLowerCase()
async updateShippingAddress_(cart, addressOrId, addrRepo) {
if (typeof addressOrId === `string`) {
addressOrId = await addrRepo.findOne({
where: { id: addressOrId },
})
}
addressOrId.country_code = addressOrId.country_code.toLowerCase()
if (
!cart.region.countries.find(({ iso_2 }) => address.country_code === iso_2)
!cart.region.countries.find(
({ iso_2 }) => addressOrId.country_code === iso_2
)
) {
throw new MedusaError(
MedusaError.Types.INVALID_DATA,
@@ -817,18 +831,23 @@ class CartService extends BaseService {
)
}
if (cart.shipping_address_id) {
const addr = await addrRepo.findOne({
where: { id: cart.shipping_address_id },
})
await addrRepo.save({ ...addr, ...address })
if (addressOrId.id) {
cart.shipping_address = addressOrId
cart.shipping_address_id = addressOrId.id
} else {
const created = addrRepo.create({
...address,
})
if (cart.shipping_address_id) {
const addr = await addrRepo.findOne({
where: { id: cart.shipping_address_id },
})
cart.shipping_address = created
await addrRepo.save({ ...addr, ...addressOrId })
} else {
const created = addrRepo.create({
...addressOrId,
})
cart.shipping_address = created
}
}
}
+1 -1
View File
@@ -226,7 +226,6 @@ class DraftOrderService extends BaseService {
/**
* Creates a draft order.
* @param {object} data - data to create draft order from
* @param {boolean} shippingRequired - needs shipping flag
* @return {Promise<DraftOrder>} the created draft order
*/
async create(data) {
@@ -348,6 +347,7 @@ class DraftOrderService extends BaseService {
const draftOrder = await this.retrieve(doId)
draftOrder.status = "completed"
draftOrder.completed_at = new Date()
draftOrder.order_id = orderId
await draftOrderRepo.save(draftOrder)
+5 -1
View File
@@ -97,8 +97,12 @@ class LineItemService extends BaseService {
const region = await this.regionService_.retrieve(regionId)
let price
let shouldMerge = true
if (config.unit_price && typeof config.unit_price !== `undefined`) {
// if custom unit_price, we ensure positive values
// and we choose to not merge the items
shouldMerge = false
if (config.unit_price < 0) {
price = 0
} else {
@@ -121,7 +125,7 @@ class LineItemService extends BaseService {
allow_discounts: !variant.product.is_giftcard,
is_giftcard: variant.product.is_giftcard,
metadata: config?.metadata || {},
should_merge: true,
should_merge: shouldMerge,
}
return toCreate