Certification Token Buy->Generate->Send

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 {};
  }
};

What part of your code doesn’t work exactly?

The part that generates the token?
The part that inserts the data?
The part that triggers the email?
Everything?
Unknown?

———-

Also, Dev Tools are available on Classic Editor. That is where they were “born” and got started. They are not a specific “feature” of Wix Studio. Regardless what the commercials say. You can code anywhere. Wix Studio premium plans cost more, has less features but can build a responsive website.

Also, the 200 emails a month is a new Wix limitation to force people to upgrade to a larger email plan. This includes regular notifications via automations that one would expect to be “free”.

1 Like

We need more restrictions! :man_facepalming:

Thanks for the second part of your reply ! I’ll take it into consideration.

For the first part, I just know that when I tried testing the code directly in the automation part, no token were created in the associated DB, I also tried in published mode but and same results.

And don’t want to trouble you more than giving me some insights if you know a way of doing it more efficiently, more simply or if there is things to consider in my code.

Thanks a lot for your reply !

Ha sorry if I didn’t something I shouldn’t !

Wasn’t my intention,

Is it about the code section ?

Try checking database permissions. Hopefully it a simple setting that needs to be fixed. If not, then you can start dissecting your code.

It’s just difficult to troubleshoot when we only see bits of pieces of code. We are simply guessing at everything else.

Ha yes !
I understand, I’ll dive into it more deeply and come back if I’m still stuck (I’ll try to be more precise)