Automated Badge Removal in Wix Studio: A Developer's Guide to User Management

Understanding Automated Badge Removal in Wix Studio

The Wix Studio community forum recently featured a topic regarding the automated removal of users from badges after a defined period. This functionality is crucial for managing memberships, certifications, or any time-sensitive access rights on a Wix Studio site. The original poster was encountering difficulties in implementing this feature using a combination of Wix Automations, Velo code, and CMS integration. This article delves into potential solutions and best practices for achieving this goal.

The Challenge: Persistent Badge Assignment

The core issue, as highlighted in the original forum post, is that once a user is assigned a badge, removing them after a specific duration, even if they are logged out or inactive, proves challenging. The user attempted a combination of Wix Automations and Velo code, along with CMS integration, but without success. The goal is to automate this process, ensuring that badge assignments are automatically revoked after the set period.

Solution: Combining Velo, Wix Automations, and the Data API

A robust solution involves leveraging Velo code to interact with the Wix Data API, coupled with Wix Automations for scheduling tasks. Here's a breakdown of the suggested approach:

  1. CMS Setup: Create a collection in your Wix CMS to manage badge assignments. This collection should include fields such as:
    • userID: The ID of the user who has been assigned the badge.
    • badgeID: The ID of the badge assigned.
    • assignmentDate: The date when the badge was assigned.
    • expiryDate: The date when the badge should be removed.
    • isActive: A boolean field indicating whether the badge is currently active (true) or not (false).
  2. Velo Backend Code: Implement a backend function using Velo to check for expired badges. This function will run periodically and update the isActive field in the CMS collection.
// Filename: backend/scheduledTasks.jsw (Wix Scheduled Tasks)
import { wixData } from 'wix-data';

export function checkExpiredBadges() {
 const now = new Date();
 return wixData.query("BadgeAssignments")
 .lt("expiryDate", now)
 .eq("isActive", true)
 .find()
 .then((results) => {
 if (results.items.length > 0) {
 const itemsToUpdate = results.items.map(item => ({
 _id: item._id,
 isActive: false
 }));

 return wixData.bulkUpdate("BadgeAssignments", itemsToUpdate)
 .then((updateResults) => {
 console.log("Updated " + updateResults.length + " expired badges.");
 })
 .catch((err) => {
 console.error(err);
 });
 } else {
 console.log("No expired badges found.");
 }
 })
 .catch((err) => {
 console.error(err);
 });
}
  1. Wix Automation: Set up a Wix Automation to trigger the checkExpiredBadges function on a daily or hourly basis. This ensures that the badge status is checked and updated regularly. To do this, create a new scheduled task in the Wix IDE and point it to the `checkExpiredBadges` function.
  2. Frontend Implementation (Optional): If you need to reflect the badge status on the frontend in real-time, you can use Velo code to query the CMS collection and display badges only for users where isActive is true.

Detailed Instructions: Step-by-Step Guide

  1. Create a CMS Collection:
    • Go to your Wix Studio dashboard and navigate to CMS.
    • Create a new collection named "BadgeAssignments".
    • Add the following fields: userID (Text), badgeID (Text), assignmentDate (Date), expiryDate (Date), and isActive (Boolean - default true).
  2. Implement the Backend Function:
    • Open the Wix IDE.
    • Create a new file in the backend folder named scheduledTasks.jsw.
    • Paste the code provided above into the file.
    • Ensure the collection name ("BadgeAssignments") in the code matches your actual collection name.
  3. Set up a Wix Automation:
    • Go to your Wix Studio dashboard and navigate to Automations.
    • Create a new automation.
    • Choose a scheduled trigger (e.g., daily at midnight).
    • As the action, select "Run a Velo task".
    • Select the checkExpiredBadges function from the scheduledTasks.jsw file.
    • Save and activate the automation.
  4. (Optional) Frontend Display:
    • On your Wix Studio page, add a dataset connected to the "BadgeAssignments" collection.
    • Use Velo code to filter the dataset to only show badges where isActive is true and the userID matches the current user's ID.

Key Considerations

  • Error Handling: Implement robust error handling in your Velo code to catch any potential issues during the data update process.
  • Performance: For sites with a large number of users and badges, consider optimizing your queries to improve performance.
  • Security: Ensure that your Velo code is secure and does not expose any sensitive data.

Conclusion

By combining the power of Velo code, Wix Automations, and the Wix Data API, you can effectively automate the removal of users from badges after a specified period in Wix Studio. This solution provides a flexible and scalable approach to managing time-sensitive access rights and ensuring that badge assignments are automatically revoked when they expire.

Start with the tools

Explore migration tools

See options, compare methods, and pick the path that fits your store.

Explore migration tools