Skip to content
Merged
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: updateSurcharge returns the entire document from db
Signed-off-by: m_sushmitha <[email protected]>
  • Loading branch information
sushmitha-malae authored and m_sushmitha committed Oct 19, 2022
commit bb93339fb4da5b1cb131a9e0bf50e502433d519d
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
13 changes: 6 additions & 7 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 { matchedCount } = await Surcharges.updateOne({
_id: surchargeId,
shopId
}, modifier);
surcharge._id = surchargeId;
surcharge.shopId = shopId;
const { matchedCount, value: updatedSurcharge } = await Surcharges.findOneAndUpdate(
{ _id: surchargeId, shopId },
modifier,
{ returnOriginal: false }
);
if (matchedCount === 0) throw new ReactionError("not-found", "Not found");

return { surcharge };
return { surcharge: 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 newSurcharge = {
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({
matchedCount: 1,
value: updatedSurcharge
}));

const result = await updateSurchargeMutation(mockContext, {
surcharge,
surchargeId: "surcharge123",
surcharge: newSurcharge,
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.surcharge).toEqual(updatedSurcharge);
});