add admin_only to shipping options
This commit is contained in:
1 parent
b65edfce06
commit
99e3e9287f
14 files changed
+50
-28
No files matched your search
@@ -40,9 +40,8 @@ export default async (req, res) => {
|
|||||||
.allow(0)
|
.allow(0)
|
||||||
.optional(),
|
.optional(),
|
||||||
})
|
})
|
||||||
.optional(),
|
.required(),
|
||||||
metadata: Validator.object().optional(),
|
metadata: Validator.object().optional(),
|
||||||
requires_shipping: Validator.boolean().default(true),
|
|
||||||
})
|
})
|
||||||
|
|
||||||
const { value, error } = schema.validate(req.body)
|
const { value, error } = schema.validate(req.body)
|
||||||
@@ -52,11 +51,7 @@ export default async (req, res) => {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
const draftOrderService = req.scope.resolve("draftOrderService")
|
const draftOrderService = req.scope.resolve("draftOrderService")
|
||||||
|
let draftOrder = await draftOrderService.create(value)
|
||||||
const requiresShipping = value.requires_shipping
|
|
||||||
delete value.requires_shipping
|
|
||||||
|
|
||||||
let draftOrder = await draftOrderService.create(value, requiresShipping)
|
|
||||||
|
|
||||||
draftOrder = await draftOrderService.retrieve(draftOrder.id, {
|
draftOrder = await draftOrderService.retrieve(draftOrder.id, {
|
||||||
relations: defaultRelations,
|
relations: defaultRelations,
|
||||||
|
|||||||
+1
@@ -39,6 +39,7 @@ describe("POST /admin/shipping-options", () => {
|
|||||||
expect(ShippingOptionServiceMock.create).toHaveBeenCalledTimes(1)
|
expect(ShippingOptionServiceMock.create).toHaveBeenCalledTimes(1)
|
||||||
expect(ShippingOptionServiceMock.create).toHaveBeenCalledWith({
|
expect(ShippingOptionServiceMock.create).toHaveBeenCalledWith({
|
||||||
is_return: false,
|
is_return: false,
|
||||||
|
admin_only: false,
|
||||||
name: "Test option",
|
name: "Test option",
|
||||||
region_id: "testregion",
|
region_id: "testregion",
|
||||||
provider_id: "test_provider",
|
provider_id: "test_provider",
|
||||||
|
|||||||
+1
@@ -11,6 +11,7 @@ const defaultFields = [
|
|||||||
"price_type",
|
"price_type",
|
||||||
"amount",
|
"amount",
|
||||||
"is_return",
|
"is_return",
|
||||||
|
"admin_only",
|
||||||
"data",
|
"data",
|
||||||
"created_at",
|
"created_at",
|
||||||
"updated_at",
|
"updated_at",
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ export default async (req, res) => {
|
|||||||
)
|
)
|
||||||
.optional(),
|
.optional(),
|
||||||
is_return: Validator.boolean().default(false),
|
is_return: Validator.boolean().default(false),
|
||||||
|
admin_only: Validator.boolean().default(false),
|
||||||
})
|
})
|
||||||
|
|
||||||
const { value, error } = schema.validate(req.body)
|
const { value, error } = schema.validate(req.body)
|
||||||
|
|||||||
@@ -34,6 +34,7 @@ export const defaultFields = [
|
|||||||
"price_type",
|
"price_type",
|
||||||
"amount",
|
"amount",
|
||||||
"is_return",
|
"is_return",
|
||||||
|
"admin_only",
|
||||||
"data",
|
"data",
|
||||||
"created_at",
|
"created_at",
|
||||||
"updated_at",
|
"updated_at",
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import { defaultFields, defaultRelations } from "./"
|
|||||||
|
|
||||||
export default async (req, res) => {
|
export default async (req, res) => {
|
||||||
try {
|
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 optionService = req.scope.resolve("shippingOptionService")
|
||||||
const data = await optionService.list(query, {
|
const data = await optionService.list(query, {
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ export default async (req, res) => {
|
|||||||
})
|
})
|
||||||
)
|
)
|
||||||
.optional(),
|
.optional(),
|
||||||
|
admin_only: Validator.boolean().optional(),
|
||||||
metadata: Validator.object().optional(),
|
metadata: Validator.object().optional(),
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ describe("GET /store/shipping-options", () => {
|
|||||||
expect(ShippingOptionServiceMock.list).toHaveBeenCalledTimes(1)
|
expect(ShippingOptionServiceMock.list).toHaveBeenCalledTimes(1)
|
||||||
expect(ShippingOptionServiceMock.list).toHaveBeenCalledWith(
|
expect(ShippingOptionServiceMock.list).toHaveBeenCalledWith(
|
||||||
{
|
{
|
||||||
|
admin_only: false,
|
||||||
profile_id: [undefined, undefined],
|
profile_id: [undefined, undefined],
|
||||||
region_id: "test-region",
|
region_id: "test-region",
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -13,6 +13,8 @@ export default async (req, res) => {
|
|||||||
query.region_id = regionId
|
query.region_id = regionId
|
||||||
}
|
}
|
||||||
|
|
||||||
|
query.admin_only = false
|
||||||
|
|
||||||
if (productIds.length) {
|
if (productIds.length) {
|
||||||
const prods = await productService.list({ id: productIds })
|
const prods = await productService.list({ id: productIds })
|
||||||
query.profile_id = prods.map(p => p.profile_id)
|
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 })
|
@Column({ default: false })
|
||||||
is_return: boolean
|
is_return: boolean
|
||||||
|
|
||||||
|
@Column({ default: false })
|
||||||
|
admin_only: boolean
|
||||||
|
|
||||||
@OneToMany(
|
@OneToMany(
|
||||||
() => ShippingOptionRequirement,
|
() => ShippingOptionRequirement,
|
||||||
req => req.shipping_option,
|
req => req.shipping_option,
|
||||||
|
|||||||
@@ -229,7 +229,7 @@ class DraftOrderService extends BaseService {
|
|||||||
* @param {boolean} shippingRequired - needs shipping flag
|
* @param {boolean} shippingRequired - needs shipping flag
|
||||||
* @return {Promise<DraftOrder>} the created draft order
|
* @return {Promise<DraftOrder>} the created draft order
|
||||||
*/
|
*/
|
||||||
async create(data, shippingRequired = true) {
|
async create(data) {
|
||||||
return this.atomicPhase_(async manager => {
|
return this.atomicPhase_(async manager => {
|
||||||
const draftOrderRepo = manager.getCustomRepository(
|
const draftOrderRepo = manager.getCustomRepository(
|
||||||
this.draftOrderRepository_
|
this.draftOrderRepository_
|
||||||
@@ -265,23 +265,21 @@ class DraftOrderService extends BaseService {
|
|||||||
})
|
})
|
||||||
|
|
||||||
let shippingMethods = []
|
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(
|
shippingMethods.push(m)
|
||||||
({ shipping_option }) => shipping_option.profile_id
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const profiles = shippingMethods.map(
|
||||||
|
({ shipping_option }) => shipping_option.profile_id
|
||||||
|
)
|
||||||
|
|
||||||
for (const item of items) {
|
for (const item of items) {
|
||||||
if (item.variant_id) {
|
if (item.variant_id) {
|
||||||
const line = await this.lineItemService_
|
const line = await this.lineItemService_
|
||||||
@@ -299,12 +297,7 @@ class DraftOrderService extends BaseService {
|
|||||||
.retrieve(item.variant_id)
|
.retrieve(item.variant_id)
|
||||||
const itemProfile = variant.product.profile_id
|
const itemProfile = variant.product.profile_id
|
||||||
|
|
||||||
let hasShipping = true
|
const hasShipping = profiles.includes(itemProfile)
|
||||||
|
|
||||||
// if shipping is required, ensure items can be shipped
|
|
||||||
if (shippingRequired) {
|
|
||||||
hasShipping = profiles.includes(itemProfile)
|
|
||||||
}
|
|
||||||
|
|
||||||
await this.lineItemService_.withTransaction(manager).create({
|
await this.lineItemService_.withTransaction(manager).create({
|
||||||
cart_id: createdCart.id,
|
cart_id: createdCart.id,
|
||||||
|
|||||||
@@ -468,6 +468,10 @@ class ShippingOptionService extends BaseService {
|
|||||||
option.name = update.name
|
option.name = update.name
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ("admin_only" in update) {
|
||||||
|
option.admin_only = update.admin_only
|
||||||
|
}
|
||||||
|
|
||||||
const optionRepo = manager.getCustomRepository(this.optionRepository_)
|
const optionRepo = manager.getCustomRepository(this.optionRepository_)
|
||||||
const result = await optionRepo.save(option)
|
const result = await optionRepo.save(option)
|
||||||
return result
|
return result
|
||||||
|
|||||||
@@ -418,6 +418,7 @@ class ShippingProfileService extends BaseService {
|
|||||||
const rawOpts = await this.shippingOptionService_.list(
|
const rawOpts = await this.shippingOptionService_.list(
|
||||||
{
|
{
|
||||||
profile_id: profileIds,
|
profile_id: profileIds,
|
||||||
|
admin_only: false,
|
||||||
},
|
},
|
||||||
{ relations: ["requirements", "profile"] }
|
{ relations: ["requirements", "profile"] }
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in new issue
Block a user