Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix] #120 : Enhance Loyalty Programs with Chimoney Integration #308

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
30 changes: 30 additions & 0 deletions submissions/Chimoney-Python/chimoney/Redeem.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,36 @@ def any(

return self._handle_request("POST", "/v0.2/redeem/any", payload)

def loyalty(self, chi_ref, loyalty_rewards, sub_account=None):
"""
This function handles the loyalty program redemption API.

Args:
chi_ref(str): The Chimoney reference for the transaction.
loyalty_rewards(list): A list of dictionaries containing the details of loyalty rewards to be redeemed.
sub_account(str): The subaccount to be used.

example:
loyalty_rewards = [
{
"rewardName": "Loyalty Reward 1",
"rewardPoints": 100,
# Add more properties as needed for your loyalty rewards.
}
]

Return:
The JSON response from the Chimoney API.
"""
payload = {
"chiRef": chi_ref,
"loyaltyRewards": loyalty_rewards,
}
if sub_account:
payload["subAccount"] = sub_account

return self._handle_request("POST", "/v0.2/redeem/loyalty", payload)

def chimoney(self, chimoneys, sub_account=None):
"""
This function handles the initiate chimoney API.
Expand Down
40 changes: 40 additions & 0 deletions submissions/chimoney-js/modules/Redeem.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,45 @@ async function chimoney(chimoneys, subAccount = null) {
});
}

/**
* This function allows loyalty program members to redeem their earned rewards as Chimoney payouts
* @param {string} chiRef The Chi reference
* @param {object} loyaltyRewards An array of loyalty rewards to be redeemed as Chimoney payouts
* @param {string?} subAccount The subAccount of the transaction
* @returns The response from the Chi Money API
*/
async function loyalty(chiRef, loyaltyRewards, subAccount = null) {
// Define validation schema
const schema = Joi.object({
chiRef: Joi.string().required(), // Chi reference should be a string and is required.
loyaltyRewards: Joi.array().items(Joi.object().keys({
// Loyalty rewards should be an array of objects with specific properties.
rewardName: Joi.string().required(), // Define the properties of the loyalty reward.
rewardPoints: Joi.number().required(),
// Add more properties as needed for your loyalty rewards.
})).min(1).required(), // At least one loyalty reward is required.
subAccount: Joi.string().allow(null).optional(), // SubAccount is optional and can be a string or null.
});

// Validate input
const { value, error } = schema.validate(
{ chiRef, loyaltyRewards, subAccount },
{ abortEarly: false }
);

if (error) throw new ValueError("input error(s)", formatJoiErrors(error));

const payload = { ...value };

if (subAccount) payload.subAccount = subAccount;

return handleRequest({
method: HTTPMETHODS.POST,
payload,
path: "/v0.2/redeem/loyalty",
});
}

/**
* This function allows you to redeem giftcard
* @param {string} chiRef The Chi reference
Expand Down Expand Up @@ -192,4 +231,5 @@ module.exports = {
giftCard,
any,
chimoney,
loyalty,
};
16 changes: 16 additions & 0 deletions submissions/chimoney-js/tests/redeem.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,20 @@ describe("Redeem", () => {
expect(error).toBeInstanceOf(ValueError);
}
});

test("loyalty: should throw Chi Money Error", async () => {
try {
await redeem.loyalty("testref", []);
} catch (error) {
expect(error).toBeInstanceOf(ChiMoneyError);
}
});

test("loyalty: should throw value error", async () => {
try {
await redeem.loyalty();
} catch (error) {
expect(error).toBeInstanceOf(ValueError);
}
});
});
Loading