Search.../

createBadge( )

Creates a badge.

Description

The createBadge() function returns a Promise that resolves to the newly created badge.

New badges do not have any badge permissions by default. You can set badge permissions from the Badges page in the Dashboard.

If backgroundColor or textColor are not specified, they default to "#796EFF" (purple) and "#FFFFFF" (white) respectively.

Admin Method

This function requires elevated permissions to run. This function is not universal and runs only on the backend.

Syntax

function createBadge(badge: Badge): Promise<CreateBadgeResponse>

createBadge Parameters

NAME
TYPE
DESCRIPTION
badge
Badge

Badge to create.

Returns

Return Type:

Promise<
CreateBadgeResponse
>
NAME
TYPE
DESCRIPTION
badge
Badge

Created badge.

Was this helpful?

Create a new badge.

Copy Code
1import { badges } from 'wix-members-backend';
2import { elevate } from 'wix-auth';
3
4/* Sample badge value:
5 * {
6 * backgroundColor: "#42A5F5",
7 * description: "Recognized Contributor Badge",
8 * icon: "http://example.com/badge-icon.svg",
9 * PermissionsEnabled: true,
10 * textColor: "#FFFFFF,
11 * title: "Contributor Badge
12 * }
13 * }
14 */
15
16export async function myCreateBadgeFunction(badge) {
17 try {
18 const elevatedCreateBadge = elevate(badges.createBadge);
19 const newBadge = await elevatedCreateBadge(badge);
20
21 console.log('New badge created: ', newBadge);
22 return newBadge;
23 } catch (error) {
24 console.error(error);
25 // Handle the error
26 }
27}
28/* Promise resolves to:
29 * {
30 * "_createdDate": "2024-01-18T13:08:22.116Z",
31 * "_id": "b025f7a8-9c3e-4f5d-a2e7-8dc1bf3ca0f1",
32 * "_updatedDate": "2024-01-18T13:08:22.116Z",
33 * "backgroundColor": "#796EFF",
34 * "description": "Recognized Contributor",
35 * "icon": "http://example.com/badge-icon.svg",
36 * "permissionsEnabled": false,
37 * "slug": "contributor-badge-1",
38 * "textColor": "#FFFFFF",
39 * "title": "Contributor Badge"
40 * }
41 */
Create an expert contributor badge.

This code automates the creation of an "Expert Contributor Badge" for users with a specific number of blog posts on the subject 'velo'.

Copy Code
1import { badges } from 'wix-members-backend';
2import { elevate } from 'wix-auth';
3import wixData from 'wix-data';
4
5/* Sample badge value:
6 * {
7 * backgroundColor: "#42A5F5",
8 * description: "Expert Contributor Badge",
9 * icon: "http://example.com/expert-badge-icon.svg",
10 * permissionsEnabled: true,
11 * textColor: "#FFFFFF",
12 * title: "Expert Contributor Badge"
13 * }
14 * }
15 */
16
17export async function createExpertContributorBadge(memberId) {
18 const EXPERT_THRESHOLD = 5; // Minimum number of blog posts to be considered an expert contributor
19 const SUBJECT_KEYWORD = 'velo'; // Subject keyword for blog posts
20
21 // Count the number of blog posts created by the member with the given ID on the subject of 'velo'
22 const blogPostsCount = await wixData.query('BlogPosts')
23 .eq('author', memberId)
24 .contains('tags', SUBJECT_KEYWORD)
25 .count();
26
27 // Count if the member meets the criteria to be an expert contributor
28 if (blogPostsCount >= EXPERT_THRESHOLD) {
29 const expertBadge = {
30 backgroundColor: "#42A5F5",
31 description: "Expert Contributor Badge",
32 icon: "http://example.com/expert-badge-icon.svg",
33 permissionsEnabled: true,
34 textColor: "#FFFFFF",
35 title: "Expert Contributor Badge"
36 };
37
38 try {
39 const elevatedCreateBadge = elevate(badges.createBadge);
40 const newExpertBadge = await elevatedCreateBadge(expertBadge);
41
42 console.log('Expert badge created for member ', memberId, ': ', newExpertBadge);
43 return newExpertBadge;
44 } catch (error) {
45 console.error('Error creating expert badge:', error);
46 // Handle the error
47 }
48 } else {
49 console.log('Member with ID ', memberId, ' does not meet the criteria for the expert badge.');
50 }
51}
52
53/* Promise resolves to:
54 * {
55 * "_createdDate": "2024-02-02T12:00:00.000Z",
56 * "_id": "unique-id-for-expert-badge-1",
57 * "_updatedDate": "2024-02-02T12:00:00.000Z",
58 * "backgroundColor": "#42A5F5",
59 * "description": "Expert Contributor Badge",
60 * "icon": "http://example.com/expert-badge-icon.svg",
61 * "permissionsEnabled": true,
62 * "slug": "expert-contributor-badge-1",
63 * "textColor": "#FFFFFF",
64 * "title": "Expert Contributor Badge"
65 * }
66 */
67