Skip to content

Commit

Permalink
Task #224395 : feat - Validate meeting links as per provider
Browse files Browse the repository at this point in the history
  • Loading branch information
Xitija committed Aug 2, 2024
1 parent f975abd commit 2009d1c
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 4 deletions.
41 changes: 41 additions & 0 deletions src/common/utils/validation.util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import {
ValidatorConstraint,
ValidatorConstraintInterface,
ValidationArguments,
} from 'class-validator';

function validateMeetingUrl(url, provider) {
const providerPatterns = {
zoom: /^https:\/\/[\w-]*\.?zoom.us\/(j|my)\/[\d\w?=-]+$/,
googlemeet: /^(https:\/\/)?meet\.google\.com\/[a-zA-Z0-9-]+$/,
// microsoftteams: /^(https:\/\/)?teams\.microsoft\.com\/[a-zA-Z0-9?&=]+$/,
// Add other supported providers as needed
};
if (
!url ||
typeof url !== 'string' ||
!provider ||
typeof provider !== 'string'
) {
return false;
}

const pattern = providerPatterns[provider.toLowerCase()];
if (!pattern) {
return false; // Unsupported provider
}

return pattern.test(url);
}

@ValidatorConstraint({ name: 'urlWithProviderValidator', async: false })
export class UrlWithProviderValidator implements ValidatorConstraintInterface {
validate(url: string, args: ValidationArguments) {
const { onlineProvider } = args.object as any;
return validateMeetingUrl(url, onlineProvider);
}

defaultMessage(args: ValidationArguments) {
return 'Invalid meeting URL for the specified provider!';
}
}
15 changes: 11 additions & 4 deletions src/modules/event/dto/create-event.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
IsIn,
} from 'class-validator';
import { ApiProperty } from '@nestjs/swagger';
import { Type } from 'class-transformer';
import { Transform, Type } from 'class-transformer';
import {
EndConditionType,
EventTypes,
Expand All @@ -31,8 +31,12 @@ import {
} from 'src/common/utils/types';
import { ERROR_MESSAGES } from 'src/common/utils/constants.util';
import { EndsWithZConstraint } from 'src/common/pipes/event-validation.pipe';
import { UrlWithProviderValidator } from 'src/common/utils/validation.util';

export class MeetingDetailsDto {
// Pass the provider from the parent DTO
onlineProvider: string;

@ApiProperty({ description: 'Meeting ID', example: 94292617 })
@IsString()
@IsNotEmpty()
Expand All @@ -44,7 +48,7 @@ export class MeetingDetailsDto {
})
@IsString()
@IsNotEmpty()
// @Validate(UrlValidator)
@Validate(UrlWithProviderValidator)
url: string;

@ApiProperty({ description: 'Meeting password', example: 'xxxxxx' })
Expand Down Expand Up @@ -253,7 +257,7 @@ export class CreateEventDto {
@ValidateIf((o) => o.eventType === EventTypes.online)
@IsString()
@IsNotEmpty()
@IsIn(['Zoom', 'GoogleMeet', 'MicrosoftTeams']) // Supported providers
@IsIn(['Zoom', 'GoogleMeet']) //, 'MicrosoftTeams' // Supported providers
onlineProvider: string;

@ApiProperty({
Expand All @@ -279,8 +283,11 @@ export class CreateEventDto {
@ValidateIf((o) => o.eventType === 'online')
@ValidateNested({ each: true })
@Type(() => MeetingDetailsDto)
@Transform(({ value, obj }) => {
value.onlineProvider = obj.onlineProvider; // Pass the provider to the nested DTO
return value;
})
meetingDetails: MeetingDetails;
// TODO: meet url validation

@ApiProperty({
type: Number,
Expand Down

0 comments on commit 2009d1c

Please sign in to comment.