-
Notifications
You must be signed in to change notification settings - Fork 0
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
base: main
Are you sure you want to change the base?
Changes from 19 commits
f998880
504948c
dc0b21a
99478ab
4cb59c0
a7c78cb
241c9f8
4ff1ab2
b17d1ad
11ea819
c3b45b1
56bd737
fa12e78
1eedf2d
84c1b30
9978cef
70a50ff
0b20547
08fecd3
4606749
d2b8fb1
d55f8fd
d3e5745
1679892
43bcca6
e5f79d4
6f180eb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,218 @@ | ||
export const createMissingCampaign = async (client) => { | ||
|
||
const createRewardPromotionObject = (firstValidationRule, secondValidationRule, thirdValidationRule) => { | ||
return { | ||
"name": "Reward Promotion", | ||
"start_date": null, | ||
"expiration_date": null, | ||
"campaign_type": "PROMOTION", | ||
"type": "STATIC", | ||
"metadata": {}, | ||
"use_voucher_metadata_schema": false, | ||
"promotion": { | ||
"tiers": [ | ||
{ | ||
"id": null, | ||
"name": "Reward Promotion Tier 1", | ||
"banner": "Add products worth $100+ to get Free Shipping", | ||
"action": { | ||
"discount": { | ||
"type": "UNIT", | ||
"amount_off": null, | ||
"unit_off": 1, | ||
"unit_type": "prod_5h1pp1ng", | ||
"effect": "ADD_MISSING_ITEMS" | ||
} | ||
}, | ||
"metadata": {}, | ||
"hierarchy": 1, | ||
"__id": "16ee1b96-0baf-4431-84a7-7dd4a7ffd46f", | ||
"active": true, | ||
"start_date": null, | ||
"expiration_date": null, | ||
"validation_rules": [ | ||
firstValidationRule ? firstValidationRule : null | ||
] | ||
}, | ||
{ | ||
"id": null, | ||
"name": "Reward Promotion Tier 2", | ||
"banner": "Add products worth $250+ for 3% discount", | ||
"action": { | ||
"discount": { | ||
"type": "PERCENT", | ||
"amount_off": null, | ||
"percent_off": 3, | ||
"effect": "APPLY_TO_ORDER" | ||
} | ||
}, | ||
"metadata": {}, | ||
"hierarchy": 2, | ||
"__id": "d3a1e546-90bc-4549-b5f2-6b6962a53b9f", | ||
"active": true, | ||
"start_date": null, | ||
"expiration_date": null, | ||
"validation_rules": [ | ||
secondValidationRule ? secondValidationRule : null | ||
] | ||
}, | ||
{ | ||
"id": null, | ||
"name": "Reward Promotion Tier 3", | ||
"banner": "Add products worth $500+ for 6% discount", | ||
"action": { | ||
"discount": { | ||
"type": "PERCENT", | ||
"amount_off": null, | ||
"percent_off": 6, | ||
"effect": "APPLY_TO_ORDER" | ||
} | ||
}, | ||
"metadata": {}, | ||
"hierarchy": 3, | ||
"__id": "a1b2c70c-3206-4c7d-84b2-614e4c43d07d", | ||
"active": true, | ||
"start_date": null, | ||
"expiration_date": null, | ||
"validation_rules": [ | ||
thirdValidationRule ? thirdValidationRule : null | ||
] | ||
} | ||
] | ||
} | ||
} | ||
} | ||
|
||
const firstValidationRule = { | ||
"id": null, | ||
"name": "Validation Rule - Reward Promotion Tier 1", | ||
"rules": { | ||
"1": { | ||
"name": "order.amount", | ||
"property": null, | ||
"conditions": { | ||
"$more_than": [ | ||
10000 | ||
] | ||
} | ||
}, | ||
"2": { | ||
"name": "order.amount", | ||
"property": null, | ||
"conditions": { | ||
"$is": [ | ||
10000 | ||
] | ||
} | ||
}, | ||
"logic": "(1 or 2)" | ||
}, | ||
"error": null, | ||
"applicable_to": { | ||
"included": [], | ||
"excluded": [], | ||
"included_all": false | ||
}, | ||
"type": "advanced", | ||
"context_type": "campaign.promotion" | ||
} | ||
|
||
const secondValidationRule = { | ||
"id": null, | ||
"name": "Validation Rule - Reward Promotion Tier 2", | ||
"rules": { | ||
"1": { | ||
"name": "order.amount", | ||
"property": null, | ||
"conditions": { | ||
"$more_than": [ | ||
25000 | ||
] | ||
} | ||
}, | ||
"2": { | ||
"name": "order.amount", | ||
"property": null, | ||
"conditions": { | ||
"$is": [ | ||
25000 | ||
] | ||
} | ||
}, | ||
"logic": "(1 or 2)" | ||
}, | ||
"error": null, | ||
"applicable_to": { | ||
"included": [], | ||
"excluded": [], | ||
"included_all": false | ||
}, | ||
"type": "advanced", | ||
"context_type": "campaign.promotion.discount.apply_to_order" | ||
} | ||
|
||
const thirdValidationRule = { | ||
"id": null, | ||
"name": "Validation Rule - Reward Promotion Tier 3", | ||
"rules": { | ||
"1": { | ||
"name": "order.amount", | ||
"property": null, | ||
"conditions": { | ||
"$more_than": [ | ||
50000 | ||
] | ||
} | ||
}, | ||
"2": { | ||
"name": "order.amount", | ||
"property": null, | ||
"conditions": { | ||
"$is": [ | ||
50000 | ||
] | ||
} | ||
}, | ||
"logic": "(1 or 2)" | ||
}, | ||
"error": null, | ||
"applicable_to": { | ||
"included": [], | ||
"excluded": [], | ||
"included_all": false | ||
}, | ||
"type": "advanced", | ||
"context_type": "campaign.promotion.discount.apply_to_order" | ||
} | ||
|
||
// const createRewardPromotion = async () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do not leave the code that is commented out |
||
// await Promise.all([ | ||
// client.validationRules.create(firstValidationRule), | ||
// client.validationRules.create(secondValidationRule), | ||
// client.validationRules.create(thirdValidationRule) | ||
// ]) | ||
// .then(([firstPromise, secondPromise, thirdPromise]) => { | ||
// const rewardPromotion = createRewardPromotionObject(firstPromise.id, secondPromise.id, thirdPromise.id); | ||
// const response = client.campaigns.create(rewardPromotion); | ||
// if (response.code !== 200) { | ||
// return new Error; | ||
// } | ||
// }) | ||
// } | ||
|
||
const createValidationRulesAndCampaign = async () => { | ||
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 createCampaignResponse = await client.campaigns.create(rewardPromotion); | ||
console.log("The Reward Campaign was successfully created."); | ||
|
||
} | ||
await createValidationRulesAndCampaign(); | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,22 @@ | ||
import express from "express"; | ||
import { accessToStackingPromotionsApp, attachEndpointsStackingPromotions } from "./stacking-promotions/server.js"; | ||
import { accessToVoucherCodeRedemptionApp, attachEndpointsVoucherCodeRedemption } from "./voucher-code-redemption/server.js"; | ||
import { accessTotieredPromotionsApp, attachEndpointsTieredCartPromotions } from "./tiered-cart-promotions/server.js"; | ||
import { addStackingPromotionRoutes } from "./stacking-promotions/server.js"; | ||
import { addEndpointsVoucherCodeRedemption } from "./voucher-code-redemption/server.js"; | ||
import { addEndpointsTieredCartPromotions } from "./tiered-cart-promotions/server.js"; | ||
import { fileURLToPath } from "url"; | ||
import "dotenv/config"; | ||
import pkg from "@voucherify/sdk"; | ||
import path from "path"; | ||
import bodyParser from "body-parser"; | ||
import { createMissingCampaign } from "./missing-campaign.js"; | ||
const { VoucherifyServerSide } = pkg; | ||
const __filename = fileURLToPath(import.meta.url); | ||
const __dirname = path.dirname(__filename); | ||
const app = express(); | ||
|
||
export const client = VoucherifyServerSide({ | ||
applicationId: `${process.env.VOUCHERIFY_APP_ID}`, | ||
secretKey : `${process.env.VOUCHERIFY_SECRET_KEY}`, | ||
// apiUrl: 'https://<region>.api.voucherify.io' | ||
applicationId: `${process.env.VOUCHERIFY_APP_ID}`, | ||
secretKey: `${process.env.VOUCHERIFY_SECRET_KEY}`, | ||
// apiUrl: 'https://<region>.api.voucherify.io' | ||
}); | ||
|
||
app.use(bodyParser.json()); | ||
|
@@ -24,36 +25,61 @@ app.use(bodyParser.urlencoded({ extended: true })); | |
app.use(express.static(path.join(__dirname, "./welcome-screen"))); | ||
|
||
app.use((req, res, next) => { | ||
res.append("Access-Control-Allow-Origin", [ "*" ]); | ||
res.append("Access-Control-Allow-Methods", "GET,PUT,POST,DELETE"); | ||
res.append("Access-Control-Allow-Headers", "Content-Type"); | ||
next(); | ||
res.append("Access-Control-Allow-Origin", ["*"]); | ||
res.append("Access-Control-Allow-Methods", "GET,PUT,POST,DELETE"); | ||
res.append("Access-Control-Allow-Headers", "Content-Type"); | ||
next(); | ||
}); | ||
|
||
const checkCredentials = async () => { | ||
try { | ||
await client.vouchers.list(); | ||
} catch (error) { | ||
if (error.code === 401) { | ||
const msg = "Your API credentials are incorrect, please check your applicationId and secretKey or visit `https://docs.voucherify.io/docs/authentication` to complete your app configuration."; | ||
throw new Error(msg); | ||
} | ||
throw new Error(error); | ||
try { | ||
await client.vouchers.list(); | ||
} catch (error) { | ||
if (error.code === 401) { | ||
const msg = "Your API credentials are incorrect, please check your applicationId and secretKey or visit `https://docs.voucherify.io/docs/authentication` to complete your app configuration."; | ||
throw new Error(msg); | ||
} | ||
throw new Error(error); | ||
} | ||
}; | ||
|
||
/* | ||
Important! | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This comment is outdated. |
||
The 'Reward Promotion' campaign is required for this project to work properly. | ||
If it doesn't exist you will see an error in the console. | ||
Visit https://github.com/voucherifyio/voucherify-examples/tree/main/tiered-cart-promotions#creating-a-reward-promotion-campaign | ||
for getting instructions on how to create the missing campaign. | ||
*/ | ||
|
||
const checkCampaign = async () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
try { | ||
await client.campaigns.get("Reward Promotion"); | ||
} catch (error) { | ||
if (error.code === 404) { | ||
try { | ||
createMissingCampaign(client); | ||
} | ||
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\nThe 'missing-campaign.js' is the script that should automatically create this campaign when the application starts (createMissingCampaign() function). | ||
\r\nIf the script doesn't work you can check the details by visiting: https://github.com/voucherifyio/voucherify-examples/tree/main/tiered-cart-promotions#creating-a-reward-promotion-campaign | ||
or you can get additional support here: https://github.com/voucherifyio/voucherify-examples/tree/main#get-support- `; | ||
throw new Error(msg); | ||
} | ||
} | ||
} | ||
} | ||
|
||
checkCredentials(); | ||
checkCampaign(); | ||
|
||
const port = process.env.PORT || 8080; | ||
|
||
app.listen(port, () => { | ||
console.log(`Hot beans app listening on port ${port}`); | ||
console.log(`Hot beans app listening on port ${port}`); | ||
}); | ||
|
||
accessToStackingPromotionsApp(app); | ||
attachEndpointsStackingPromotions(app, client); | ||
|
||
accessToVoucherCodeRedemptionApp(app); | ||
attachEndpointsVoucherCodeRedemption(app, client); | ||
|
||
accessTotieredPromotionsApp(app); | ||
attachEndpointsTieredCartPromotions(app, client); | ||
addStackingPromotionRoutes(app, client); | ||
addEndpointsVoucherCodeRedemption(app, client); | ||
addEndpointsTieredCartPromotions(app, client); |
There was a problem hiding this comment.
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 maybenode add-missing-campaign
?