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. (dashboard page code)

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

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