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

Refactor request #4

Open
wants to merge 27 commits into
base: main
Choose a base branch
from
Open

Refactor request #4

wants to merge 27 commits into from

Conversation

weronika-kurczyna
Copy link

  1. Made refactor in other endpoints (merge 2 functions in 1), reduce the code.
  2. Deleted the alert message because it makes no sense. If the campaign doesn't exists, the script won't run and the error will occur.
  3. Add clear information about missing campaign in main server.js, in console (error message) and the instruction in main readme and tiered-cart-promotion readme.

Marcin Slezak and others added 13 commits November 10, 2022 17:09
…utes", remove "accessToStackingPromotionApp". Change names of "promotionStackableObj" and "defaultItems". Move "validateStackableParams" and "customer" objects to "addStackingPromotionRoutes" function.
Add validateStackableParams and redeemStackableParams instead of one global object
Assigning params directly in object.
I deleted the alert because it started generating bugs when checking if the rewardPromotion exists.
README.md Outdated
- Keep the returned validation_rule id after executing each request. It will be required for the main request.

```
curl --location --request POST 'https://api.voucherify.io/v1/validation-rules' \
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As we discussed on Friday, a better option will be to write a small Nodejs CLI script, as then we can automate putting generated validation_rule id to campaign endpoint.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here, you probably want to convince developer to use missing-campaign.js script instead of showing curls

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

main README: no cURLs and
Note: The application should automatically add the missing campaign "Reward Promotion" by using script called "missing-campaign.js", so there is no need to add it manually.

README id tiered-cart-promotions:
Information previously added by Patryk + info about 'missing-campaign' + I left the cURL there "just in case", but I can also delete them from the second README.

server.js Outdated
@@ -41,19 +41,31 @@ const checkCredentials = async () => {
throw new Error(error);
}
};

//Important!
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be great to put a little attention to how we format code comments:

//Some comment

vs 

// Some comment with space before the first letter

//Multi line
//comment without a space at the beginning
// and starting with slashes 

vs

/*
  Multiline (block) comments with proper spacing
  make it easier to read.  
*/

}

const createRewardPromotion = async () => {
await Promise.all([
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mixed two approaches:

chaining promises

const result1 = await somePromise1();
const result2 = await somePromise2();
const result3 = await somePromise3();

parallel promises

const results = await Promise.all([
  somePromise1(),
  somePromise2(),
  somePromise3()
])

You also mixed async/await syntax with then syntax.

And you miss promise handling for client.campaigns.create

At the end:

const validationRules = await Promise.all([
  client.validationRules.create(firstValidationRule),
  client.validationRules.create(secondValidationRule),
  client.validationRules.create(thirdValidationRule),
])

const rewardPromotion = createRewardPromotionObject(validationRules[0].id, validationRules[1].id, validationRules[2].id);

const response = await client.campaigns.create(rewardPromotion);

if (response.code !== 200) {
  return new Error;
}

Copy link
Author

@weronika-kurczyna weronika-kurczyna Nov 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed, but have to check one thing before pushing

README.md Outdated
- Keep the returned validation_rule id after executing each request. It will be required for the main request.

```
curl --location --request POST 'https://api.voucherify.io/v1/validation-rules' \
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here, you probably want to convince developer to use missing-campaign.js script instead of showing curls

- Keep the returned validation_rule id after executing each request. It will be required for the main request.

```
curl --location --request POST 'https://api.voucherify.io/v1/validation-rules' \
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing-campaign.js instead of curls?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

up

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do not need to describe curl details if we have a JS script. Just leave a information that you need to run a script if you have missing campaigns

server.js Outdated
console.log("The Reward Campaign was successfully created.");
}
catch (error) {
const msg = "The 'Reward Promotion' campaign not found. This campaign is required for 'tiered-cart-promotion' to work properly. \r\nPlease create a 'Reward Promotion' campaign first. \r\nYou can check the details by visiting: https://github.com/voucherifyio/voucherify-examples/tree/main/tiered-cart-promotions#creating-a-reward-promotion-campaign";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggest missing-campaign.js ?

README.md Outdated
@@ -53,6 +53,8 @@ npm run start / npm run dev || yarn start / yarn run dev
```
7. Go to [http://localhost:3000](http://localhost:3000/) in your browser.

Note: The application should automatically add the missing campaign "Reward Promotion" by using script called "missing-campaign.js", so there is no need to add it manually.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

node missing-campaign? Also, it would be great to add a verb into the script name as we do some action, so maybe node add-missing-campaign?

"context_type": "campaign.promotion.discount.apply_to_order"
}

// const createRewardPromotion = async () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do not leave the code that is commented out

From now if an error from V% occurs, it will be logged to the console, but also the informative "msg" defined by me.
@@ -0,0 +1,207 @@
export const addMissingCampaign = async (client) => {

const createRewardPromotionObject = (firstValidationRule, secondValidationRule, thirdValidationRule) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do not have typescript support here, so even more important is to pay attention to naming. Please note that the createRewardPromotionObject function accepts as parameters id of three validation rules, not validation rule definitions from below. Therefore, the firstValidationRule name has two meanings in that file, once its rule id, and once its rule definition. I suggest changing a name from firstValidationRule to firstValidationRuleId

@@ -0,0 +1,207 @@
export const addMissingCampaign = async (client) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the idea of using closure to provide V% client 👍 Please note that only the addValidationRulesAndCampaign function needs that context. The createRewardPromotionObject, firstValidationRule, secondValidationRule and thirdValidationRule functions do not need this context so they can be put outside of the outer function.

server.js Outdated
};

/*
Important!
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is outdated.

for getting instructions on how to create the missing campaign.
*/

const checkCampaign = async () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The checkCampaign function actually adds a campaign to the V% account if it does not exists, which may be a little dangerous if someone by accident connects the production account. I suggest does not allow to run the example (like for checkCredentials) and displaying the message that you have already in the catch block.

- Keep the returned validation_rule id after executing each request. It will be required for the main request.

```
curl --location --request POST 'https://api.voucherify.io/v1/validation-rules' \
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

up

- Keep the returned validation_rule id after executing each request. It will be required for the main request.

```
curl --location --request POST 'https://api.voucherify.io/v1/validation-rules' \
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do not need to describe curl details if we have a JS script. Just leave a information that you need to run a script if you have missing campaigns

- Changed the naming from "firstValidationRule" to "firstValidationRuleId" etc.
- Deleted unnecessary comments and text in Readme
- Formatted code with tslint formatter
- Moved "createRewardPromotionObject" and validation rules object outside the outer function
@@ -0,0 +1,17 @@
{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file should be excluded from the repo

server.js Outdated
await client.campaigns.get("Reward Promotion");
} catch (error) {
if (error.code === 404) {
const msg = "The 'Reward Promotion' campaign not found. \r\nPlease run the command 'node ./add-missing-campaign.js' in the terminal to create the missing campaign. \r\nWhen you receive a message that the campaign has been successfully created you can run the main script again. \r\nYou can get additional support here: https://github.com/voucherifyio/voucherify-examples/tree/main#get-support- \r\n";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a little hard to read it. Also, \r\n is Windows-specific. Maybe:

const { EOL } = require('os');
 // ...
const msgs = [
  'The \'Reward Promotion\' campaign not found',
  'Please run the command \'node ./add-missing-campaign.js\' in the terminal to create the missing campaign.',
  'When you receive a message that the campaign has been successfully created you can run the main script again.',
  'You can get additional support here: https://github.com/voucherifyio/voucherify-examples/tree/main#get-support'
]

// try
console.log(msgs.join(EOL))
// or
console.log(...msgs)

Copy link
Author

@weronika-kurczyna weronika-kurczyna Jan 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestion to use EOL! 🙂

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changes have been made

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants