Remove user from badge after a period

Wix Studio

I want the user to be removed from the badge after a set period, even if they are logged out or active on the site.

Firstly, what do you mean by a ā€˜set period’? 30 days after signing up? Or two weeks after purchasing a product or a plan? Or on a specific date like 25th December?

Given on what your ā€˜set period’ is, you will either have to set up an automation or write a background cron job which:

  1. Calculates the period and filters out members who match that criteria.
  2. Then using the unassignBadge() function, pass the Badge ID and the ID of all the members that you want to remove the badge for.

Hello,

Thank you for your reply. I speak French, so I’ll translate into English.

In Automation, I want to implement the following process:
Form submitted → badge issued to the user → 2-minute delay (for testing, for example) → badge removed.

The problem is that it gets stuck at the last step: there’s no way in Automation to remove a badge, except by using Velo code. That’s precisely where it doesn’t work. I tried using ChatGPT (https://chatgpt.com/share/6950dfcb-2e0c-8000-ae02-ebc2f6d038be) to help, but I can’t configure that part correctly.

That’s correct. Automations is indeed missing an ā€œUnassign Badgeā€ function. So now the only way to do this is through code, by using the function I pointed out above. All you need to do is hardcode the ID of the badge that you want to remove. If you can provide the ID of that badge, I can help you out with the code.

Hello,

Thank you for your reply. Here is the ID of the badge in question. As previously mentioned, I would like the badge to be automatically removed from each person who has it, for example, 2 minutes after it is issued.

Please note that each member may receive the badge at a different time; therefore, removal must be managed individually for each user.

Badge ID: 9254490d-ac87-4c34-ac37-e2c20bb299fe

Here’s how to add the code to unassign the badge. Tried and tested on my end, should work perfectly if you follow the steps correctly:

  1. Open up your automation that is triggered when a new user signs up.
  2. In the next step, assign the Badge.
  3. Then add your delay.
  4. Then choose the following action: Run Velo Code.
  5. Click on Start Coding, name the file removeBadge and then click on Create & Start Coding.

  1. Then carefully replace the default code with the one I have provided below:
/**
 * Autocomplete function declaration, do not delete
 * @param {import('./__schema__.js').Payload} options
 */

import { badges } from "wix-members.v2";
import { elevate } from "wix-auth";

export const invoke = async ({ payload }) => {
    const badgeId = "9254490d-ac87-4c34-ac37-e2c20bb299fe";
    const memberId = payload.contactId;
    try {
        const elevatedUnassignBadge = elevate(badges.unassignBadge);
        await elevatedUnassignBadge(badgeId, [memberId]);
    } catch (error) {
        console.error(error);
    }
    return {}
};
  1. Make sure there are no red lines in the code. Note that your specific badge ID has been hardcoded as a variable.

  1. Finally, click on Save, and then click on Apply.
  2. Check that all the steps in your automation have a checkmark as seen in the image below; if not, choose the particular step and click Apply.

  1. Finally, click on the Publish button on the top right corner to publish your automation.

Now your members’ badges will be removed automatically after the delay that you’ve set.

The automation will run individually for each user signup so yes the badge will be removed independently for each user after the set delay even if they are logged out or inactive.

Note:

To ensure this works perfectly, make sure there’s no other code, event hook or automation that will interfere with this. If you have added the code that was suggested by ChatGPT which did not work, I’d recommend that you either disable it by commenting the code, or remove it completely.

Happy coding!

1 Like

Thanks so much, everything works!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.