-
Notifications
You must be signed in to change notification settings - Fork 5
feat: implement timezone functionality #28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Ethan225300
wants to merge
1
commit into
dev
Choose a base branch
from
feat/CP-31-User-Profile-update-timezone
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| * text=auto |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,4 +19,4 @@ describe('AppController', () => { | |
| expect(appController.healthCheck()).toBe('Hello World!'); | ||
| }); | ||
| }); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| export interface ITimezone { | ||
| id: string; // "America/New_York" | ||
| displayName: string; // "Eastern Standard Time (EST)" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| import { majorTimezones,convertToUTC, convertToLocalTime,getFilteredMajorTimezones } from './time-utils'; | ||
| import * as moment from 'moment-timezone'; | ||
|
|
||
| describe('time-utils', () => { | ||
| it('should convert local time to UTC correctly', () => { | ||
| // Use moment to create a local time with an explicit time zone | ||
| const localTime = moment.tz('2023-05-01T12:00:00', 'America/New_York').toDate(); | ||
| const timezone = 'America/New_York'; | ||
| const expected = '2023-05-01T16:00:00.000Z'; // New York Daylight Time, UTC-4 | ||
|
|
||
| const result = convertToUTC(localTime, timezone); | ||
|
|
||
| expect(result.toISOString()).toEqual(expected); | ||
| }); | ||
|
|
||
| it('should convert UTC time to local time correctly', () => { | ||
| const utcTime = new Date('2023-05-01T16:00:00Z'); | ||
| const timezone = 'America/New_York'; | ||
| const expected = moment.tz('2023-05-01T12:00:00', 'America/New_York').toISOString(); | ||
| const result = convertToLocalTime(utcTime, timezone); | ||
| expect(result.toISOString()).toEqual(expected); | ||
| }); | ||
| it('should filter major timezones correctly', () => { | ||
| const filteredTimezones = getFilteredMajorTimezones(); | ||
| expect(filteredTimezones.length).toBeLessThanOrEqual(majorTimezones.length); | ||
| filteredTimezones.forEach(timezone => { | ||
| expect(majorTimezones).toContain(timezone); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| import * as moment from 'moment-timezone'; | ||
| import 'moment-timezone/builds/moment-timezone-with-data'; | ||
|
|
||
| export const majorTimezones: string[] = [ | ||
| 'UTC', // Coordinated Universal Time | ||
| 'America/New_York', // Eastern Time (US & Canada) | ||
| 'America/Chicago', // Central Time (US & Canada) | ||
| 'America/Denver', // Mountain Time (US & Canada) | ||
| 'America/Los_Angeles', // Pacific Time (US & Canada) | ||
| 'America/Anchorage', // Alaska Time | ||
| 'America/Honolulu', // Hawaii Time | ||
| 'America/Sao_Paulo', // Brazil Time | ||
| 'America/Bogota', // Colombia Time | ||
| 'America/Buenos_Aires', // Argentina Time | ||
| 'Europe/London', // United Kingdom Time | ||
| 'Europe/Berlin', // Germany Time (represents Central European Time) | ||
| 'Europe/Moscow', // Moscow Time (Russia) | ||
| 'Europe/Athens', // Greece Time | ||
| 'Europe/Istanbul', // Turkey Time | ||
| 'Africa/Cairo', // Egypt Time | ||
| 'Africa/Johannesburg', // South Africa Time | ||
| 'Africa/Lagos', // Nigeria Time | ||
| 'Asia/Shanghai', // China Standard Time | ||
| 'Asia/Tokyo', // Japan Time | ||
| 'Asia/Kolkata', // India Time | ||
| 'Asia/Dubai', // United Arab Emirates Time | ||
| 'Asia/Bangkok', // Thailand Time | ||
| 'Asia/Jakarta', // Jakarta Time (Indonesia) | ||
| 'Australia/Sydney', // Eastern Australia Time | ||
| 'Australia/Perth', // Western Australia Time | ||
| 'Pacific/Auckland', // New Zealand Time | ||
| 'Pacific/Fiji', // Fiji Time | ||
| 'Europe/Stockholm', // Sweden Time (represents Scandinavian Time) | ||
| ]; | ||
|
|
||
| export function convertToUTC(localTime: Date, timezone: string): Date { | ||
| return moment(localTime).tz(timezone).utc().toDate(); | ||
| } | ||
|
|
||
| export function convertToLocalTime(utcTime: Date, timezone: string): Date { | ||
| return moment.utc(utcTime).tz(timezone).toDate(); | ||
| } | ||
| // Get the filtered primary time zone | ||
| export function getFilteredMajorTimezones(): string[] { | ||
| const allTimezones = moment.tz.names(); | ||
| return allTimezones.filter(timezone => majorTimezones.includes(timezone)); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Ethan225300 marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -63,4 +63,4 @@ export class UserService { | |
| }); | ||
| return res; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import { ObjectType, Field } from '@nestjs/graphql'; | ||
|
|
||
| @ObjectType() | ||
| export class UpdateTimezoneResponse { | ||
| @Field(() => Boolean) | ||
| success: boolean; | ||
|
|
||
| @Field(() => String) | ||
| message: string; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| import { Module } from '@nestjs/common'; | ||
| import { TimezoneService } from './timezone.service'; | ||
| import { AuthModule } from '../modules/auth/auth.module'; | ||
| import { TimezoneResolver } from './timezone.resolver'; | ||
| import { UserModule } from '../modules/user/user.module'; | ||
|
|
||
| @Module({ | ||
| imports: [AuthModule, UserModule], | ||
| providers: [TimezoneService, TimezoneResolver], | ||
| exports: [TimezoneService] | ||
| }) | ||
| export class TimezoneModule {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| import { Test, TestingModule } from '@nestjs/testing'; | ||
| import { TimezoneResolver } from './timezone.resolver'; | ||
| import { TimezoneService } from './timezone.service'; | ||
| import { UnauthorizedException } from '@nestjs/common'; | ||
|
|
||
| describe('TimezoneResolver', () => { | ||
| let resolver: TimezoneResolver; | ||
| let timezoneService: TimezoneService; | ||
|
|
||
| beforeEach(async () => { | ||
| const module: TestingModule = await Test.createTestingModule({ | ||
| providers: [ | ||
| TimezoneResolver, | ||
| { | ||
| provide: TimezoneService, | ||
| useValue: { | ||
| getAllTimezones: jest.fn().mockReturnValue([ | ||
| { id: 'UTC', displayName: 'Coordinated Universal Time (UTC)' }, | ||
| { id: 'GMT', displayName: 'Greenwich Mean Time (GMT)' }, | ||
| { id: 'Asia/Shanghai', displayName: 'China Standard Time (CST)' } | ||
| ]), | ||
| getMajorTimezones: jest.fn().mockReturnValue([ | ||
| { id: 'UTC', displayName: 'Coordinated Universal Time (UTC)' } | ||
| ]), | ||
| updateUserTimezone: jest.fn().mockResolvedValue({ | ||
| success: true, | ||
| message: 'Timezone updated successfully.', | ||
| }), | ||
| }, | ||
| }, | ||
| ], | ||
| }).compile(); | ||
|
|
||
| resolver = module.get<TimezoneResolver>(TimezoneResolver); | ||
| timezoneService = module.get<TimezoneService>(TimezoneService); | ||
| }); | ||
|
|
||
| it('should be defined', () => { | ||
| expect(resolver).toBeDefined(); | ||
| expect(timezoneService).toBeDefined(); | ||
| }); | ||
|
|
||
| describe('getTimezones', () => { | ||
| it('should return a list of all available timezones', () => { | ||
| const result = resolver.getTimezones(); | ||
| expect(result).toEqual(['Coordinated Universal Time (UTC)', 'Greenwich Mean Time (GMT)', 'China Standard Time (CST)']); | ||
| expect(timezoneService.getAllTimezones).toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('getMajorTimezones', () => { | ||
| it('should return a list of major timezones', () => { | ||
| const result = resolver.getMajorTimezones(); | ||
| expect(result).toEqual(['Coordinated Universal Time (UTC)']); | ||
| expect(timezoneService.getMajorTimezones).toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('updateTimezone', () => { | ||
| it('should return a successful update message', async () => { | ||
| const userId = 'some-uuid'; | ||
| const timezone = 'Asia/Tokyo'; | ||
| const req = { user: { id: userId }, headers: { authorization: 'Bearer some-valid-jwt-token' } }; | ||
| const context = { req }; | ||
|
|
||
| const result = await resolver.updateTimezone(userId, timezone, context); | ||
| expect(result).toEqual({ | ||
| success: true, | ||
| message: 'Timezone updated successfully.', | ||
| }); | ||
| expect(timezoneService.updateUserTimezone).toHaveBeenCalledWith(userId, timezone); | ||
| }); | ||
|
|
||
| it('should throw an UnauthorizedException if user ids do not match', async () => { | ||
| const userId = 'some-uuid'; | ||
| const timezone = 'Asia/Tokyo'; | ||
| const req = { user: { id: 'different-uuid' }, headers: { authorization: 'Bearer some-valid-jwt-token' } }; | ||
| const context = { req }; | ||
|
|
||
| await expect(resolver.updateTimezone(userId, timezone, context)).rejects.toThrow(UnauthorizedException); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| import { UnauthorizedException, UseGuards } from '@nestjs/common'; | ||
| import { Resolver, Query, Mutation, Args, Context } from '@nestjs/graphql'; | ||
| import { TimezoneService } from './timezone.service'; | ||
| import { UpdateTimezoneResponse } from './dto/update-timezone-response.dto'; | ||
| import { GqlAuthGuard } from '../common/guards/auth.guard'; | ||
|
|
||
| @Resolver() | ||
| export class TimezoneResolver { | ||
| constructor(private readonly timezoneService: TimezoneService) {} | ||
|
|
||
| @Query(() => [String], { | ||
| description: 'Get a list of all available timezones', | ||
| }) | ||
| getTimezones(): string[] { | ||
| return this.timezoneService.getAllTimezones().map(tz => tz.displayName); | ||
| } | ||
|
|
||
| @Query(() => [String], { | ||
| description: 'Get a list of major timezones', | ||
| }) | ||
| getMajorTimezones(): string[] { | ||
| return this.timezoneService.getMajorTimezones().map(tz => tz.displayName); | ||
| } | ||
|
|
||
| @Mutation(() => UpdateTimezoneResponse) | ||
| @UseGuards(GqlAuthGuard) | ||
| async updateTimezone( | ||
| @Args('userId') userId: string, | ||
| @Args('timezone') timezone: string, | ||
| @Context() context: any | ||
| ): Promise<UpdateTimezoneResponse> { | ||
| const req = context.req; | ||
| const user = req.user; | ||
|
|
||
| if (user.id !== userId) { | ||
| throw new UnauthorizedException('User authentication failed'); | ||
| } | ||
|
|
||
| return this.timezoneService.updateUserTimezone(userId, timezone); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.