add admin_only to shipping options
This commit is contained in:
@@ -40,9 +40,8 @@ export default async (req, res) => {
|
||||
.allow(0)
|
||||
.optional(),
|
||||
})
|
||||
.optional(),
|
||||
.required(),
|
||||
metadata: Validator.object().optional(),
|
||||
requires_shipping: Validator.boolean().default(true),
|
||||
})
|
||||
|
||||
const { value, error } = schema.validate(req.body)
|
||||
@@ -52,11 +51,7 @@ export default async (req, res) => {
|
||||
|
||||
try {
|
||||
const draftOrderService = req.scope.resolve("draftOrderService")
|
||||
|
||||
const requiresShipping = value.requires_shipping
|
||||
delete value.requires_shipping
|
||||
|
||||
let draftOrder = await draftOrderService.create(value, requiresShipping)
|
||||
let draftOrder = await draftOrderService.create(value)
|
||||
|
||||
draftOrder = await draftOrderService.retrieve(draftOrder.id, {
|
||||
relations: defaultRelations,
|
||||
|
||||
@@ -39,6 +39,7 @@ describe("POST /admin/shipping-options", () => {
|
||||
expect(ShippingOptionServiceMock.create).toHaveBeenCalledTimes(1)
|
||||
expect(ShippingOptionServiceMock.create).toHaveBeenCalledWith({
|
||||
is_return: false,
|
||||
admin_only: false,
|
||||
name: "Test option",
|
||||
region_id: "testregion",
|
||||
provider_id: "test_provider",
|
||||
|
||||
@@ -11,6 +11,7 @@ const defaultFields = [
|
||||
"price_type",
|
||||
"amount",
|
||||
"is_return",
|
||||
"admin_only",
|
||||
"data",
|
||||
"created_at",
|
||||
"updated_at",
|
||||
|
||||
@@ -23,6 +23,7 @@ export default async (req, res) => {
|
||||
)
|
||||
.optional(),
|
||||
is_return: Validator.boolean().default(false),
|
||||
admin_only: Validator.boolean().default(false),
|
||||
})
|
||||
|
||||
const { value, error } = schema.validate(req.body)
|
||||
|
||||
@@ -34,6 +34,7 @@ export const defaultFields = [
|
||||
"price_type",
|
||||
"amount",
|
||||
"is_return",
|
||||
"admin_only",
|
||||
"data",
|
||||
"created_at",
|
||||
"updated_at",
|
||||
|
||||
@@ -3,7 +3,7 @@ import { defaultFields, defaultRelations } from "./"
|
||||
|
||||
export default async (req, res) => {
|
||||
try {
|
||||
const query = _.pick(req.query, ["region_id", "is_return"])
|
||||
const query = _.pick(req.query, ["region_id", "is_return", "admin_only"])
|
||||
|
||||
const optionService = req.scope.resolve("shippingOptionService")
|
||||
const data = await optionService.list(query, {
|
||||
|
||||
@@ -20,6 +20,7 @@ export default async (req, res) => {
|
||||
})
|
||||
)
|
||||
.optional(),
|
||||
admin_only: Validator.boolean().optional(),
|
||||
metadata: Validator.object().optional(),
|
||||
})
|
||||
|
||||
|
||||
@@ -26,6 +26,7 @@ describe("GET /store/shipping-options", () => {
|
||||
expect(ShippingOptionServiceMock.list).toHaveBeenCalledTimes(1)
|
||||
expect(ShippingOptionServiceMock.list).toHaveBeenCalledWith(
|
||||
{
|
||||
admin_only: false,
|
||||
profile_id: [undefined, undefined],
|
||||
region_id: "test-region",
|
||||
},
|
||||
|
||||
@@ -13,6 +13,8 @@ export default async (req, res) => {
|
||||
query.region_id = regionId
|
||||
}
|
||||
|
||||
query.admin_only = false
|
||||
|
||||
if (productIds.length) {
|
||||
const prods = await productService.list({ id: productIds })
|
||||
query.profile_id = prods.map(p => p.profile_id)
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
import { MigrationInterface, QueryRunner } from "typeorm"
|
||||
|
||||
export class shippingOptionAdminOnly1613896971521
|
||||
implements MigrationInterface {
|
||||
name = "shippingOptionAdminOnly1613896971521"
|
||||
|
||||
public async up(queryRunner: QueryRunner): Promise<void> {
|
||||
await queryRunner.query(
|
||||
`ALTER TABLE "shipping_option" ADD "admin_only" boolean NOT NULL DEFAULT false`
|
||||
)
|
||||
}
|
||||
|
||||
public async down(queryRunner: QueryRunner): Promise<void> {
|
||||
await queryRunner.query(
|
||||
`ALTER TABLE "shipping_option" DROP COLUMN "admin_only"`
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -70,6 +70,9 @@ export class ShippingOption {
|
||||
@Column({ default: false })
|
||||
is_return: boolean
|
||||
|
||||
@Column({ default: false })
|
||||
admin_only: boolean
|
||||
|
||||
@OneToMany(
|
||||
() => ShippingOptionRequirement,
|
||||
req => req.shipping_option,
|
||||
|
||||
@@ -229,7 +229,7 @@ class DraftOrderService extends BaseService {
|
||||
* @param {boolean} shippingRequired - needs shipping flag
|
||||
* @return {Promise<DraftOrder>} the created draft order
|
||||
*/
|
||||
async create(data, shippingRequired = true) {
|
||||
async create(data) {
|
||||
return this.atomicPhase_(async manager => {
|
||||
const draftOrderRepo = manager.getCustomRepository(
|
||||
this.draftOrderRepository_
|
||||
@@ -265,23 +265,21 @@ class DraftOrderService extends BaseService {
|
||||
})
|
||||
|
||||
let shippingMethods = []
|
||||
let profiles = []
|
||||
if (shippingRequired) {
|
||||
for (const method of shipping_methods) {
|
||||
const m = await this.shippingOptionService_
|
||||
.withTransaction(manager)
|
||||
.createShippingMethod(method.option_id, method.data, {
|
||||
cart: createdCart,
|
||||
})
|
||||
|
||||
shippingMethods.push(m)
|
||||
}
|
||||
for (const method of shipping_methods) {
|
||||
const m = await this.shippingOptionService_
|
||||
.withTransaction(manager)
|
||||
.createShippingMethod(method.option_id, method.data, {
|
||||
cart: createdCart,
|
||||
})
|
||||
|
||||
profiles = shippingMethods.map(
|
||||
({ shipping_option }) => shipping_option.profile_id
|
||||
)
|
||||
shippingMethods.push(m)
|
||||
}
|
||||
|
||||
const profiles = shippingMethods.map(
|
||||
({ shipping_option }) => shipping_option.profile_id
|
||||
)
|
||||
|
||||
for (const item of items) {
|
||||
if (item.variant_id) {
|
||||
const line = await this.lineItemService_
|
||||
@@ -299,12 +297,7 @@ class DraftOrderService extends BaseService {
|
||||
.retrieve(item.variant_id)
|
||||
const itemProfile = variant.product.profile_id
|
||||
|
||||
let hasShipping = true
|
||||
|
||||
// if shipping is required, ensure items can be shipped
|
||||
if (shippingRequired) {
|
||||
hasShipping = profiles.includes(itemProfile)
|
||||
}
|
||||
const hasShipping = profiles.includes(itemProfile)
|
||||
|
||||
await this.lineItemService_.withTransaction(manager).create({
|
||||
cart_id: createdCart.id,
|
||||
|
||||
@@ -468,6 +468,10 @@ class ShippingOptionService extends BaseService {
|
||||
option.name = update.name
|
||||
}
|
||||
|
||||
if ("admin_only" in update) {
|
||||
option.admin_only = update.admin_only
|
||||
}
|
||||
|
||||
const optionRepo = manager.getCustomRepository(this.optionRepository_)
|
||||
const result = await optionRepo.save(option)
|
||||
return result
|
||||
|
||||
@@ -418,6 +418,7 @@ class ShippingProfileService extends BaseService {
|
||||
const rawOpts = await this.shippingOptionService_.list(
|
||||
{
|
||||
profile_id: profileIds,
|
||||
admin_only: false,
|
||||
},
|
||||
{ relations: ["requirements", "profile"] }
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user