Skip to content
Prev Previous commit
Next Next commit
fix: using update and find functions
  • Loading branch information
m_sushmitha committed Oct 19, 2022
commit 3751ecac6c550acecdf8dc67593748cb538b4a11
17 changes: 8 additions & 9 deletions packages/api-plugin-surcharges/src/mutations/updateSurcharge.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,12 @@ export default async function updateSurchargeMutation(context, input) {

Surcharge.validate(modifier, { modifier: true });

const { modifiedCount, value: updatedSurcharge } = await Surcharges.findOneAndUpdate({
_id: surchargeId,
shopId
}, modifier, {
returnOriginal: false
});
if (modifiedCount === 0 || !updatedSurcharge) throw new ReactionError("not-found", "Unable to update surcharge");

return { updatedSurcharge };
const { matchedCount } = await Surcharges.updateOne({
_id: surchargeId, shopId
}, modifier);
if (matchedCount === 0) throw new ReactionError("not-found", "Unable to update surcharge");

const updatedSurcharge = await Surcharges.findOne({ _id: surchargeId, shopId });

return updatedSurcharge;
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const updatedSurcharge = {
]
};

const newSubcharge = {
const newSurcharge = {
type: "surcharge",
attributes: [
{ property: "vendor", value: "john", propertyType: "string", operator: "eq" },
Expand All @@ -51,14 +51,16 @@ const newSubcharge = {
};

test("update a surcharge", async () => {
mockContext.collections.Surcharges.findOneAndUpdate.mockReturnValueOnce(Promise.resolve({
modifiedCount: 1,
value: updatedSurcharge
mockContext.collections.Surcharges.updateOne.mockReturnValueOnce(Promise.resolve({
matchedCount: 1
}));
mockContext.collections.Surcharges.findOne.mockReturnValueOnce(Promise.resolve({
updatedSurcharge
}));

const result = await updateSurchargeMutation(mockContext, {
surchargeId: "surcharge123",
surcharge: newSubcharge,
surcharge: newSurcharge,
shopId: "shop123"
});

Expand Down