Skip to content

Commit

Permalink
Task #223114 chore: resolved timezone issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Xitija committed Jul 15, 2024
1 parent 634c04b commit d4425d8
Show file tree
Hide file tree
Showing 6 changed files with 183 additions and 137 deletions.
106 changes: 75 additions & 31 deletions src/common/pipes/event-validation.pipe.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,45 @@
import { ConfigService } from '@nestjs/config';
import { PipeTransform, Injectable, BadRequestException, forwardRef, Inject } from '@nestjs/common';
import {
PipeTransform,
Injectable,
BadRequestException,
forwardRef,
Inject,
} from '@nestjs/common';
import { CreateEventDto } from 'src/modules/event/dto/create-event.dto';
import { getTimezoneCurrentDate } from '../utils/pipe.util';
import { getTimezoneDate } from '../utils/pipe.util';
import { get } from 'http';
import { ERROR_MESSAGES } from '../utils/constants.util';
import { ValidatorConstraint, ValidatorConstraintInterface, ValidationArguments } from 'class-validator';
import {
ValidatorConstraint,
ValidatorConstraintInterface,
ValidationArguments,
} from 'class-validator';
@Injectable()
export class DateValidationPipe implements PipeTransform {
// constructor(@Inject(forwardRef(() => ConfigService)) private configService: ConfigService) {
// constructor(@Inject(forwardRef(() => ConfigService)) private configService: ConfigService) {

// }

transform(createEventDto: CreateEventDto) {
const timeZone = 'Asia/Kolkata';
const startDate = new Date(createEventDto.startDatetime);
const endDate = new Date(createEventDto.endDatetime);
const currentDate = getTimezoneCurrentDate(timeZone) // Current date
const startDate = getTimezoneDate(
timeZone,
new Date(createEventDto.startDatetime),
);
const endDate = getTimezoneDate(
timeZone,
new Date(createEventDto.endDatetime),
);
const currentDate = getTimezoneDate(timeZone); // Current date
// this.configService.get<string>('TIMEZONE'); // Get the timezone from the config service

console.log('currentDate', currentDate);
console.log('startDate', startDate);
console.log('endDate', endDate);

console.log(startDate <= currentDate, 'startDate <= currentDate');

if (startDate <= currentDate) {
throw new BadRequestException(
'Start date must be today or a future date',
Expand All @@ -39,43 +57,80 @@ export class DateValidationPipe implements PipeTransform {
@Injectable()
export class RegistrationDateValidationPipe implements PipeTransform {
transform(createEventDto: CreateEventDto) {
const currentDate = getTimezoneCurrentDate('Asia/Kolkata');
const startDate = new Date(createEventDto.startDatetime);
const endDate = new Date(createEventDto.endDatetime);
const registrationStartDate = new Date(
createEventDto.registrationStartDate,
const timeZone = 'Asia/Kolkata';
const currentDate = getTimezoneDate(timeZone);
const startDate = getTimezoneDate(
timeZone,
new Date(createEventDto.startDatetime),
);
const endDate = getTimezoneDate(
timeZone,
new Date(createEventDto.endDatetime),
);
const registrationStartDate = createEventDto.registrationEndDate
? getTimezoneDate(
timeZone,
new Date(createEventDto.registrationStartDate),
)
: null;
const isRestricted = createEventDto.isRestricted;
const registrationEndDate = createEventDto.registrationEndDate
? getTimezoneDate(timeZone, new Date(createEventDto.registrationEndDate))
: null;

console.log(
registrationStartDate,
'rrrrr',
startDate,
registrationStartDate > startDate,
registrationStartDate < startDate,
);
console.log(
createEventDto.isRestricted && registrationStartDate,
'createEventDto.isRestricted && registrationStartDate ',
);
console.log(
createEventDto.isRestricted && registrationEndDate,
'createEventDto.isRestricted && registrationEndDate',
);
const registrationEndDate = new Date(createEventDto.registrationEndDate);
if (
(createEventDto.isRestricted && registrationStartDate) ||
(createEventDto.isRestricted && registrationEndDate)
) {
console.log('');
throw new BadRequestException(
ERROR_MESSAGES.RESTRICTED_EVENT_NO_REGISTRATION_DATE,
);
}

// Ensure registration dates are not in the past
if (registrationStartDate < currentDate) {
if (registrationStartDate < currentDate && !isRestricted) {
throw new BadRequestException(
ERROR_MESSAGES.REGISTRATION_START_DATE_INVALID,
);
}

if (registrationEndDate < currentDate) {
if (registrationEndDate < currentDate && !isRestricted) {
throw new BadRequestException(
ERROR_MESSAGES.REGISTRATION_END_DATE_INVALID,
);
}

// Validate registration dates
if (registrationStartDate > registrationEndDate) {
if (registrationStartDate > registrationEndDate && !isRestricted) {
throw new BadRequestException(
ERROR_MESSAGES.REGISTRATION_START_DATE_BEFORE_END_DATE,
);
}

// Registration period must fall between the event period
console.log(registrationStartDate, "rrrrr", startDate, registrationStartDate > startDate, registrationStartDate < startDate)
if (registrationStartDate > startDate) {
if (registrationStartDate > startDate && !isRestricted) {
throw new BadRequestException(
ERROR_MESSAGES.REGISTRATION_START_DATE_BEFORE_EVENT_DATE,
);
}

if (registrationEndDate > startDate) {
if (registrationEndDate > startDate && !isRestricted) {
throw new BadRequestException(
ERROR_MESSAGES.REGISTRATION_END_DATE_BEFORE_EVENT_DATE,
);
Expand All @@ -87,7 +142,7 @@ export class RegistrationDateValidationPipe implements PipeTransform {

export class RecurringEndDateValidationPipe implements PipeTransform {
transform(createEventDto: CreateEventDto) {
const currentDate = getTimezoneCurrentDate('Asia/Kolkata');
const currentDate = getTimezoneDate('Asia/Kolkata');
if (createEventDto.isRecurring) {
const recurrenceEndDate = new Date(createEventDto.recurrenceEndDate);
const startDate = new Date(createEventDto.startDatetime);
Expand Down Expand Up @@ -151,14 +206,3 @@ export class ParamsValidationPipe implements PipeTransform {
}
}
}

@ValidatorConstraint({ name: 'endsWithZ', async: false })
export class EndsWithZConstraint implements ValidatorConstraintInterface {
validate(text: string, args: ValidationArguments) {
return typeof text === 'string' && text.endsWith('Z');
}

defaultMessage(args: ValidationArguments) {
return '($value) must end with "Z"';
}
}
14 changes: 8 additions & 6 deletions src/common/utils/pipe.util.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
export function getTimezoneCurrentDate(timeZone: string) {
const nowUtc = new Date();
const nowTimezone = new Date(nowUtc.toLocaleString('en-US', { timeZone: timeZone }));
export function getTimezoneDate(timeZone: string, nowUtc: Date = new Date()) {
// converts the current date(UTC) to the timezone -> 2024-07-15T11:07:09.827Z => 2024-07-15T16:37:09.827Z
const nowTimezone = new Date(
nowUtc.toLocaleString('en-US', { timeZone: timeZone }),
);

const offset = nowTimezone.getTimezoneOffset() * 60000;
const offset = nowTimezone.getTimezoneOffset() * 60000;

return new Date(nowTimezone.getTime() - offset) //.toISOString() //.slice(0, -1);
}
return new Date(nowTimezone.getTime() - offset); //.toISOString() //.slice(0, -1);
}
34 changes: 17 additions & 17 deletions src/common/utils/response-interface.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
// structure for server responses
export interface ServerResponse {
// api id
id: string;
// api id
id: string;

// response param
params: Params;
// response param
params: Params;

// response code
responseCode: string;
// response code
responseCode: string;

//server result
result: any;
//server result
result: any;

// time stamp
ts: string;
// time stamp
ts: string;

// api version
ver: string;
// api version
ver: string;

headers?: any;
headers?: any;
}

export interface Params {
resmsgid: string;
error?: string;
status: string;
errmsg?: string;
resmsgid: string;
error?: string;
status: string;
errmsg?: string;
}
98 changes: 49 additions & 49 deletions src/common/utils/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,58 +2,58 @@ import { v4 } from 'uuid';
import { ServerResponse, Params } from './response-interface';

export default class APIResponse {
public static success<Type>(
id: string,
result: Type,
statusCode: string,
): ServerResponse {
try {
const params: Params = {
resmsgid: v4(),
status: 'successful',
error: null,
errmsg: null,
};
public static success<Type>(
id: string,
result: Type,
statusCode: string,
): ServerResponse {
try {
const params: Params = {
resmsgid: v4(),
status: 'successful',
error: null,
errmsg: null,
};

const resObj: ServerResponse = {
id,
ver: '1.0',
ts: new Date().toISOString(),
params,
responseCode: statusCode,
result,
};
return resObj;
} catch (e) {
return e;
}
const resObj: ServerResponse = {
id,
ver: '1.0',
ts: new Date().toISOString(),
params,
responseCode: statusCode,
result,
};
return resObj;
} catch (e) {
return e;
}
}

public static error(
id: string,
errmsg: string,
error: string,
statusCode: string,
): ServerResponse {
try {
const params: Params = {
resmsgid: v4(),
status: 'failed',
errmsg: errmsg,
error,
};
public static error(
id: string,
errmsg: string,
error: string,
statusCode: string,
): ServerResponse {
try {
const params: Params = {
resmsgid: v4(),
status: 'failed',
errmsg: errmsg,
error,
};

const resObj: ServerResponse = {
id,
ver: '1.0',
ts: new Date().toISOString(),
params,
responseCode: statusCode,
result: {},
};
return resObj;
} catch (e) {
return e;
}
const resObj: ServerResponse = {
id,
ver: '1.0',
ts: new Date().toISOString(),
params,
responseCode: statusCode,
result: {},
};
return resObj;
} catch (e) {
return e;
}
}
}
19 changes: 9 additions & 10 deletions src/common/utils/types.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
export enum EventTypes {
online = 'online',
offline = 'offline',
online = 'online',
offline = 'offline',
}

export enum EventStatus {
active = 'active',
inactive = 'inactive',
completed = 'completed',
active = 'active',
inactive = 'inactive',
completed = 'completed',
}

export type MeetingDetails = {
id: string;
url: string;
password: string;
}

id: string;
url: string;
password: string;
};
Loading

0 comments on commit d4425d8

Please sign in to comment.