From 1a5721b7fa129c56e9487982ffe92daeb3121715 Mon Sep 17 00:00:00 2001 From: Souvik Mukhopadhyay <146748505+Souvik9205@users.noreply.github.com> Date: Thu, 9 Jan 2025 22:22:34 +0530 Subject: [PATCH] feat(schema): Add workspace invitation schema (#612) --- packages/schema/src/workspace/index.ts | 22 +++++++ packages/schema/src/workspace/index.types.ts | 12 +++- packages/schema/tests/workspace.spec.ts | 69 +++++++++++++++++++- 3 files changed, 101 insertions(+), 2 deletions(-) diff --git a/packages/schema/src/workspace/index.ts b/packages/schema/src/workspace/index.ts index d9bc8805..97546dda 100644 --- a/packages/schema/src/workspace/index.ts +++ b/packages/schema/src/workspace/index.ts @@ -149,3 +149,25 @@ export const GlobalSearchResponseSchema = z.object({ }) ) }) + +export const GetWorkspaceInvitationsRequest = PageRequestSchema + +export const GetWorkspaceInvitationsResponse = PageResponseSchema( + z.object({ + workspace: z.object({ + id: z.string(), + name: z.string(), + slug: z.string(), + icon: z.string().nullable() + }), + roles: z.array( + z.object({ + role: z.object({ + name: z.string(), + colorCode: z.string().nullable() + }) + }) + ), + invitedOn: z.string().datetime() + }) +) diff --git a/packages/schema/src/workspace/index.types.ts b/packages/schema/src/workspace/index.types.ts index 97b025d1..776834b3 100644 --- a/packages/schema/src/workspace/index.types.ts +++ b/packages/schema/src/workspace/index.types.ts @@ -15,7 +15,9 @@ import { ExportDataRequestSchema, ExportDataResponseSchema, GlobalSearchRequestSchema, - GlobalSearchResponseSchema + GlobalSearchResponseSchema, + GetWorkspaceInvitationsRequest, + GetWorkspaceInvitationsResponse } from '.' import { PageRequestSchema } from '..' @@ -66,3 +68,11 @@ export type ExportDataResponse = z.infer export type GlobalSearchRequest = z.infer export type GlobalSearchResponse = z.infer + +export type GetWorkspaceInvitationsRequest = z.infer< + typeof GetWorkspaceInvitationsRequest +> + +export type GetWorkspaceInvitationsResponse = z.infer< + typeof GetWorkspaceInvitationsResponse +> diff --git a/packages/schema/tests/workspace.spec.ts b/packages/schema/tests/workspace.spec.ts index 9bea3e09..3da473b3 100644 --- a/packages/schema/tests/workspace.spec.ts +++ b/packages/schema/tests/workspace.spec.ts @@ -14,7 +14,9 @@ import { ExportDataResponseSchema, GetAllWorkspacesOfUserResponseSchema, GlobalSearchResponseSchema, - GetAllWorkspacesOfUserRequestSchema + GetAllWorkspacesOfUserRequestSchema, + GetWorkspaceInvitationsRequest, + GetWorkspaceInvitationsResponse } from '@/workspace' describe('Workspace Schema Tests', () => { @@ -511,4 +513,69 @@ describe('Workspace Schema Tests', () => { ]) }) }) + + describe('GetInvitationRequestSchema Tests', () => { + it('should validate when correct page and limit are provided for GetInvitationRequestSchema', () => { + const result = GetWorkspaceInvitationsRequest.safeParse({ + page: 1, + limit: 10 + }) + + expect(result.success).toBe(true) + }) + }) + + describe('GetInvitationResponseSchema Tests', () => { + it('should validate with proper input for GetInvitationResponseSchema', () => { + const result = GetWorkspaceInvitationsResponse.safeParse({ + items: [ + { + workspace: { + id: 'workspace-id', + name: 'Workspace Name', + slug: 'workspace-slug', + icon: null + }, + roles: [ + { + role: { + name: 'Admin', + colorCode: '#FFFFFF' + } + } + ], + invitedOn: '2023-12-01T12:00:00Z' + } + ], + metadata: { + page: 1, + perPage: 10, + pageCount: 1, + totalCount: 1, + links: { + self: 'https://example.com/self', + first: 'https://example.com/first', + previous: null, + next: null, + last: 'https://example.com/last' + } + } + }) + + expect(result.success).toBe(true) + }) + + it('should not validate if items are not an array in GetInvitationResponseSchema', () => { + const result = GetWorkspaceInvitationsResponse.safeParse({ + items: 'not-an-array', + metadata: { + page: 1, + perPage: 10 + } + }) + + expect(result.success).toBe(false) + expect(result.error?.issues[0].path).toEqual(['items']) + }) + }) })