Remove ProductService dependency from ProductVariantService
This commit is contained in:
@@ -1,6 +1,3 @@
|
|||||||
module.exports = {
|
module.exports = {
|
||||||
testEnvironment: "node",
|
testEnvironment: "node",
|
||||||
testPathIgnorePatterns: [
|
|
||||||
"mocks"
|
|
||||||
]
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -312,36 +312,6 @@ describe("ProductVariantService", () => {
|
|||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
it("throw error if product with variant does not exist", async () => {
|
|
||||||
try {
|
|
||||||
await productVariantService.addOptionValue(
|
|
||||||
IdMap.getId("failId"),
|
|
||||||
IdMap.getId("testOptionId"),
|
|
||||||
"testValue"
|
|
||||||
)
|
|
||||||
} catch (err) {
|
|
||||||
expect(err.message).toEqual(
|
|
||||||
`Products with variant: ${IdMap.getId("failId")} was not found`
|
|
||||||
)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
it("throw error if product does not have option id", async () => {
|
|
||||||
try {
|
|
||||||
await productVariantService.addOptionValue(
|
|
||||||
IdMap.getId("testVariant"),
|
|
||||||
IdMap.getId("failOptionId"),
|
|
||||||
"testValue"
|
|
||||||
)
|
|
||||||
} catch (err) {
|
|
||||||
expect(err.message).toEqual(
|
|
||||||
`Associated product does not have option: ${IdMap.getId(
|
|
||||||
"failOptionId"
|
|
||||||
)}`
|
|
||||||
)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
it("throw error if option value is not string", async () => {
|
it("throw error if option value is not string", async () => {
|
||||||
try {
|
try {
|
||||||
await productVariantService.addOptionValue(
|
await productVariantService.addOptionValue(
|
||||||
@@ -379,21 +349,6 @@ describe("ProductVariantService", () => {
|
|||||||
{ $pull: { options: { option_id: IdMap.getId("testing") } } }
|
{ $pull: { options: { option_id: IdMap.getId("testing") } } }
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
it("throw error if product still has the option id of the option value we are trying to delete", async () => {
|
|
||||||
try {
|
|
||||||
await productVariantService.deleteOptionValue(
|
|
||||||
IdMap.getId("testVariant"),
|
|
||||||
IdMap.getId("testOptionId")
|
|
||||||
)
|
|
||||||
} catch (err) {
|
|
||||||
expect(err.message).toEqual(
|
|
||||||
`Associated product has option with id: ${IdMap.getId(
|
|
||||||
"testOptionId"
|
|
||||||
)}`
|
|
||||||
)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("delete", () => {
|
describe("delete", () => {
|
||||||
|
|||||||
@@ -308,22 +308,6 @@ class ProductVariantService extends BaseService {
|
|||||||
* @return {Promise} the result of the update operation.
|
* @return {Promise} the result of the update operation.
|
||||||
*/
|
*/
|
||||||
async addOptionValue(variantId, optionId, optionValue) {
|
async addOptionValue(variantId, optionId, optionValue) {
|
||||||
const products = await this.productService_.list({ variants: variantId })
|
|
||||||
if (!products.length) {
|
|
||||||
throw new MedusaError(
|
|
||||||
MedusaError.Types.NOT_FOUND,
|
|
||||||
`Products with variant: ${variantId} was not found`
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
const product = products[0]
|
|
||||||
if (!product.options.find(o => o._id === optionId)) {
|
|
||||||
throw new MedusaError(
|
|
||||||
MedusaError.Types.NOT_FOUND,
|
|
||||||
`Associated product does not have option: ${optionId}`
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
const variant = await this.retrieve(variantId)
|
const variant = await this.retrieve(variantId)
|
||||||
|
|
||||||
if (typeof optionValue !== "string" && typeof optionValue !== "number") {
|
if (typeof optionValue !== "string" && typeof optionValue !== "number") {
|
||||||
@@ -350,22 +334,6 @@ class ProductVariantService extends BaseService {
|
|||||||
* @return {Promise} the result of the update operation.
|
* @return {Promise} the result of the update operation.
|
||||||
*/
|
*/
|
||||||
async deleteOptionValue(variantId, optionId) {
|
async deleteOptionValue(variantId, optionId) {
|
||||||
const products = await this.productService_.list({ variants: variantId })
|
|
||||||
if (!products.length) {
|
|
||||||
throw new MedusaError(
|
|
||||||
MedusaError.Types.NOT_FOUND,
|
|
||||||
`Products with variant: ${variantId} was not found`
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
const product = products[0]
|
|
||||||
if (product.options.find(o => o._id === optionId)) {
|
|
||||||
throw new MedusaError(
|
|
||||||
MedusaError.Types.INVALID_DATA,
|
|
||||||
`Associated product has option with id: ${optionId}`
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
return this.productVariantModel_.updateOne(
|
return this.productVariantModel_.updateOne(
|
||||||
{ _id: variantId },
|
{ _id: variantId },
|
||||||
{ $pull: { options: { option_id: optionId } } }
|
{ $pull: { options: { option_id: optionId } } }
|
||||||
|
|||||||
Reference in New Issue
Block a user