Hello,
My goal is to make a certification token e-shop,
the customer should be able to:
- buy tokens
- receive them by email or check them in an account page
- use them in an other website
In the back :
- Generate new tokens when some are bought
- Save them in a database
- Send them in an email (optional)
I looked a little and found old topics with manual implementation but not that specific and a little bit old .
I moved my website to Wix Studio because I realized that it was really difficult without developer tools,
I’m a developer but never used JS before.
I tried using Automation withVelo code but 2 problems arrize:
- A limit of 200 email a mounth
- I don’t know why but the code doesn’t seem to work… I double checked…
Here is the Velo code in automation after something is “PAID”:
import wixData from 'wix-data';
// Mapping item names to prefixes
const tokenPrefixes = {
"certification Agile": "AG",
"certification Kanban": "KB",
"certification Kaizen": "KZ"
};
const generateToken = (prefix) => {
const timestamp = Date.now();
const randomPart = Math.random().toString(36).substring(2, 15);
return `${prefix}-${randomPart}-${timestamp}`;
};
const processLineItems = (lineItems,contactId) => {
let tokens = [];
lineItems.forEach(item => {
const prefix = tokenPrefixes[item.itemName];
if (prefix) {
for (let i = 0; i < item.quantity; i++) {
tokens.push({
typeJeton: item.itemName,
jeton: generateToken(prefix),
dateCreation: new Date(),
idAcheteur: contactId,
statutJeton: 'available'
});
}
}
});
return tokens;
};
const saveTokensToDatabase = async (tokens) => {
const insertPromises = tokens.map(tokenInfo => wixData.insert('Tokens', tokenInfo));
return Promise.all(insertPromises);
};
export const invoke = async ({ payload }) => {
try {
// Process line items from the order and generate tokens
const tokens = processLineItems(payload.lineItems, payload.contactId);
await saveTokensToDatabase(tokens);
return {};
} catch (error) {
console.error("Error in token creation:", error);
return {};
}
};