Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix: update surcharge mutation will send the whole item
  • Loading branch information
m_sushmitha committed Oct 18, 2022
commit 473f399828d7a26a5c0d71609a73401d34a95ba0
5 changes: 5 additions & 0 deletions .changeset/updateSurcharge-return-full-item.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@reactioncommerce/api-plugin-surcharges": patch
---

Fixed Update surcharge mutation to return the whole item from db
12 changes: 6 additions & 6 deletions packages/api-plugin-surcharges/src/mutations/updateSurcharge.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ export default async function updateSurchargeMutation(context, input) {

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

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

return { surcharge };
return { updatedSurcharge };
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,36 @@ mockContext.validatePermissions.mockReturnValueOnce(Promise.resolve(null));

const createdAt = new Date();

const surcharge = {
const updatedSurcharge = {
updatedAt: jasmine.any(Date),
type: "surcharge",
attributes: [
{ property: "vendor", value: "reaction", propertyType: "string", operator: "eq" },
{ property: "productType", value: "knife", propertyType: "string", operator: "eq" }
{ property: "vendor", value: "john", propertyType: "string", operator: "eq" },
{ property: "productType", value: "gun", propertyType: "string", operator: "eq" }
],
createdAt,
destination: { region: ["CO", "NY"] },
amount: 5.99,
destination: { region: ["NJ", "WY"] },
amount: 17.99,
messagesByLanguage: [
{
content: "Original Message English",
content: "Updated Message English",
language: "en"
}, {
content: "Original Message Spanish",
content: "Updated Message Spanish",
language: "es"
}
]
};

const updatedSurcharge = {
const newSubcharge = {
type: "surcharge",
attributes: [
{ property: "vendor", value: "john", propertyType: "string", operator: "eq" },
{ property: "productType", value: "gun", propertyType: "string", operator: "eq" }
],
createdAt,
destination: { region: ["NJ", "WY"] },
amount: {
amount: 17.99,
currencyCode: "USD"
},
amount: 17.99,
messagesByLanguage: [
{
content: "Updated Message English",
Expand All @@ -53,38 +51,16 @@ const updatedSurcharge = {
};

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

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

expect(result).toEqual({
surcharge: {
_id: "surcharge123",
shopId: "shop123",
type: "surcharge",
attributes: [
{ property: "vendor", value: "reaction", propertyType: "string", operator: "eq" },
{ property: "productType", value: "knife", propertyType: "string", operator: "eq" }
],
createdAt,
destination: { region: ["CO", "NY"] },
amount: 5.99,
messagesByLanguage: [
{
content: "Original Message English",
language: "en"
}, {
content: "Original Message Spanish",
language: "es"
}
]
}
});
expect(result.updatedSurcharge).toEqual(updatedSurcharge);
});