Skip to content

Commit

Permalink
feat: setup the participant when initialize the room
Browse files Browse the repository at this point in the history
  • Loading branch information
carlossantos74 committed Jan 6, 2025
1 parent e877c35 commit 80ef952
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/room/src/common/types/participant.types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export type InitialParticipant = {
id: string
name: string
email?: string
}

export type Participant = InitialParticipant & {
Expand Down
75 changes: 75 additions & 0 deletions packages/room/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Room } from './core';
import { ApiService } from './services/api';
import config from './services/config';

import { createRoom } from '.';
Expand All @@ -8,6 +9,8 @@ jest.mock('./services/api', () => ({
validateApiKey: jest.fn(() => Promise.resolve(true)),
fetchWaterMark: jest.fn(() => Promise.resolve({})),
fetchLimits: jest.fn(() => Promise.resolve({})),
createParticipant: jest.fn(() => Promise.resolve()),
fetchParticipant: jest.fn(() => Promise.resolve(null)),
},
}));

Expand Down Expand Up @@ -254,4 +257,76 @@ describe('createRoom', () => {

expect(newRoom).not.toBe(room);
});

test('should throw an error if the participant name is missing and the participant does not exist in the API', async () => {
const params = {
developerToken: 'abc123',
roomId: 'abc123',
participant: {
id: 'abc123',
},
group: {
id: 'abc123',
name: 'Group',
},
environment: 'dev' as 'dev',
};

const createRoomPromise = createRoom(params);

await expect(createRoomPromise).rejects.toThrow(
'[SuperViz | Room] - Participant does not exist, create the user in the API or add the name in the initialization to initialize the SuperViz room.',
);
});

test('should ignore the participant name if the participant exists in the API', async () => {
const params = {
developerToken: 'abc123',
roomId: 'abc123',
participant: {
id: 'abc123',
},
group: {
id: 'abc123',
name: 'Group',
},
environment: 'dev' as 'dev',
};

jest.spyOn(ApiService, 'fetchParticipant').mockResolvedValueOnce({
id: 'abc123',
name: 'John Doe',
email: '[email protected]',
});

room = await createRoom(params);

expect(room).toBeInstanceOf(Room);
});

test('should create the participant if the participant does not exist in the API', async () => {
const params = {
developerToken: 'abc123',
roomId: 'abc123',
participant: {
id: 'abc123',
name: 'John Doe',
},
group: {
id: 'abc123',
name: 'Group',
},
environment: 'dev' as 'dev',
};

jest.spyOn(ApiService, 'fetchParticipant').mockResolvedValueOnce(null);

room = await createRoom(params);

expect(ApiService.createParticipant).toHaveBeenCalledWith({
participantId: 'abc123',
name: 'John Doe',
email: undefined,
});
});
});
27 changes: 26 additions & 1 deletion packages/room/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import debug from 'debug';
import { z } from 'zod';

import { Participant } from './common/types/participant.types';
import { InitialParticipant, Participant } from './common/types/participant.types';
import { Room } from './core';
import { Callback, ParticipantEvent, RoomEvent } from './core/types';
import { ApiService } from './services/api';
Expand Down Expand Up @@ -57,6 +57,30 @@ async function setUpEnvironment({
config.set('waterMark', waterMark);
}

async function setUpParticipant(participant: InitialParticipant): Promise<InitialParticipant> {
const apiParticipant = await ApiService.fetchParticipant(participant.id).catch(() => null);

if (!apiParticipant && !participant.name) {
throw new Error(
'[SuperViz | Room] - Participant does not exist, create the user in the API or add the name in the initialization to initialize the SuperViz room.',
);
}

if (!apiParticipant) {
await ApiService.createParticipant({
participantId: participant.id,
name: participant?.name,
email: participant?.email,
});
}

return {
id: participant.id,
name: participant.name ?? apiParticipant?.name,
email: participant.email ?? apiParticipant?.email,
};
}

/**
* @description Creates a new room with the given parameters.
* @param {InitializeRoomParams} params - The parameters required to initialize the room.
Expand All @@ -70,6 +94,7 @@ export async function createRoom(params: InitializeRoomParams): Promise<Room> {
const { participant } = InitializeRoomSchema.parse(params);

await setUpEnvironment(params);
await setUpParticipant(participant as InitialParticipant);

if (typeof window !== 'undefined' && window.SUPERVIZ_ROOM) {
console.warn(`[SuperViz | Room] An existing room instance was found in the window object.
Expand Down
16 changes: 16 additions & 0 deletions packages/room/src/services/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import config from '../config';
import { ComponentLimits } from '../config/types';

import { CreateParticipantParams } from './types';

export class ApiService {
static async doRequest(url: string, method: string, body: any, customHeaders = {}) {
const response = await fetch(url, {
Expand Down Expand Up @@ -51,4 +53,18 @@ export class ApiService {
const { message } = await this.doRequest(url, 'POST', { apiKey });
return message;
}

static async createParticipant(
participant: CreateParticipantParams,
): Promise<void> {
const path = '/participants';
const url = this.createUrl(path);
return this.doRequest(url, 'POST', { ...participant }, { apikey: config.get<string>('apiKey') });
}

static async fetchParticipant(id: string) {
const path = `/participants/${id}`;
const url = this.createUrl(path);
return this.doRequest(url, 'GET', undefined, { apikey: config.get<string>('apiKey') });
}
}
6 changes: 6 additions & 0 deletions packages/room/src/services/api/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export type CreateParticipantParams = {
name?: string;
participantId: string;
avatar?: string | null;
email?: string
};
3 changes: 2 additions & 1 deletion packages/room/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ export const InitializeRoomSchema = z.object({
roomId: z.string().regex(pattern, { message: '[SuperViz | Room] Room id is invalid, it should be between 2 and 64 characters and only accept letters, numbers and special characters: -_&@+=,(){}[]/«».|\'"' }),
participant: z.object({
id: z.string().regex(pattern, { message: '[SuperViz | Room] Participant id is invalid, it should be between 2 and 64 characters and only accept letters, numbers and special characters: -_&@+=,(){}[]/«».|\'"' }),
name: z.string(),
name: z.string().optional(),
email: z.string().email().optional(),
}),
group: z.object({
id: z.string().regex(pattern, { message: '[SuperViz | Room] Group id is invalid, it should be between 2 and 64 characters and only accept letters, numbers and special characters: -_&@+=,(){}[]/«».|\'"' }),
Expand Down

0 comments on commit 80ef952

Please sign in to comment.