From 325c9e5b40a36f5aedb3c1e28bcb1954253aa868 Mon Sep 17 00:00:00 2001 From: Oluwaseyi Idowu Date: Sat, 24 Aug 2024 04:24:36 +0100 Subject: [PATCH 1/9] feat: get a single comment --- src/modules/comments/comments.controller.ts | 10 +++++++++- src/modules/comments/comments.service.ts | 15 +++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/modules/comments/comments.controller.ts b/src/modules/comments/comments.controller.ts index 24adfd0f0..ae50f4450 100644 --- a/src/modules/comments/comments.controller.ts +++ b/src/modules/comments/comments.controller.ts @@ -1,4 +1,4 @@ -import { Controller, Body, Post, Request } from '@nestjs/common'; +import { Controller, Body, Post, Request, Get, Param } from '@nestjs/common'; import { CommentsService } from './comments.service'; import { CreateCommentDto } from './dtos/create-comment.dto'; import { CommentResponseDto } from './dtos/comment-response.dto'; @@ -18,4 +18,12 @@ export class CommentsController { const { userId } = req.user; return await this.commentsService.addComment(createCommentDto, userId); } + + @ApiOperation({ summary: 'Get a comment' }) + @ApiResponse({ status: 200, description: 'The comment has been retrieved successfully.' }) + @Get(':id') + async getAComment(@Param('id') id: string, @Request() req): Promise { + const { userId } = req.user; + return await this.commentsService.getAComment(id, userId); + } } diff --git a/src/modules/comments/comments.service.ts b/src/modules/comments/comments.service.ts index f2f2e7633..cbccb8e4c 100644 --- a/src/modules/comments/comments.service.ts +++ b/src/modules/comments/comments.service.ts @@ -42,4 +42,19 @@ export class CommentsService { commentedBy, }; } + + async getAComment(commentId: string, userId: string) { + const user = await this.userRepository.findOne({ where: { id: userId } }); + if (!user) { + throw new CustomHttpException('User not found', HttpStatus.NOT_FOUND); + } + const comment = await this.commentRepository.findOneBy({ id: commentId }); + if (!comment) { + throw new CustomHttpException('Comment not found', HttpStatus.NOT_FOUND); + } + return { + message: 'Comment retrieved successfully', + data: { comment }, + }; + } } From d8b94d993d531df56cc0751dd9897a15640b2008 Mon Sep 17 00:00:00 2001 From: Oluwaseyi Idowu Date: Sat, 24 Aug 2024 15:24:00 +0100 Subject: [PATCH 2/9] fix: reduce data fetching for performance --- src/modules/comments/comments.service.ts | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/modules/comments/comments.service.ts b/src/modules/comments/comments.service.ts index cbccb8e4c..cad87ebac 100644 --- a/src/modules/comments/comments.service.ts +++ b/src/modules/comments/comments.service.ts @@ -43,11 +43,7 @@ export class CommentsService { }; } - async getAComment(commentId: string, userId: string) { - const user = await this.userRepository.findOne({ where: { id: userId } }); - if (!user) { - throw new CustomHttpException('User not found', HttpStatus.NOT_FOUND); - } + async getAComment(commentId: string) { const comment = await this.commentRepository.findOneBy({ id: commentId }); if (!comment) { throw new CustomHttpException('Comment not found', HttpStatus.NOT_FOUND); From 737d7eccb550521c221f26da55f698293f162187 Mon Sep 17 00:00:00 2001 From: Oluwaseyi Idowu Date: Sat, 24 Aug 2024 15:37:10 +0100 Subject: [PATCH 3/9] fix: update controller --- package.json | 4 ++-- src/modules/comments/comments.controller.ts | 5 ++--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index b83f48286..6e7c1e539 100644 --- a/package.json +++ b/package.json @@ -33,8 +33,8 @@ "postinstall": "npm install --platform=linux --arch=x64 sharp" }, "dependencies": { - "@css-inline/css-inline-linux-x64-gnu": "^0.14.1", "@css-inline/css-inline": "^0.14.1", + "@css-inline/css-inline-linux-x64-gnu": "^0.14.1", "@faker-js/faker": "^8.4.1", "@google/generative-ai": "^0.17.0", "@nestjs-modules/mailer": "^2.0.2", @@ -74,7 +74,7 @@ "pg": "^8.12.0", "reflect-metadata": "^0.2.2", "rxjs": "^7.8.1", - "sharp": "^0.33.4", + "sharp": "^0.33.5", "speakeasy": "^2.0.0", "supertest": "^7.0.0", "typeorm": "^0.3.20", diff --git a/src/modules/comments/comments.controller.ts b/src/modules/comments/comments.controller.ts index ae50f4450..a0d6f48dd 100644 --- a/src/modules/comments/comments.controller.ts +++ b/src/modules/comments/comments.controller.ts @@ -22,8 +22,7 @@ export class CommentsController { @ApiOperation({ summary: 'Get a comment' }) @ApiResponse({ status: 200, description: 'The comment has been retrieved successfully.' }) @Get(':id') - async getAComment(@Param('id') id: string, @Request() req): Promise { - const { userId } = req.user; - return await this.commentsService.getAComment(id, userId); + async getAComment(@Param('id') id: string): Promise { + return await this.commentsService.getAComment(id); } } From 9ff440c3bf6cca9d4678be0e8faa32cd6c05eacd Mon Sep 17 00:00:00 2001 From: Prudent Bird Date: Sat, 24 Aug 2024 20:27:47 +0100 Subject: [PATCH 4/9] chore: refactor google authentication --- src/modules/auth/auth.controller.ts | 6 +-- src/modules/auth/auth.module.ts | 7 +-- src/modules/auth/auth.service.ts | 48 +++++++++++++------ src/modules/auth/dto/create-user.dto.ts | 9 ++++ src/modules/auth/google-auth.service.ts | 25 ---------- .../interfaces/GoogleAuthPayloadInterface.ts | 18 ------- .../GoogleVerificationPayloadInterface.ts | 27 ----------- .../auth/strategies/google.strategy.ts | 48 ------------------- 8 files changed, 48 insertions(+), 140 deletions(-) delete mode 100644 src/modules/auth/google-auth.service.ts delete mode 100644 src/modules/auth/interfaces/GoogleVerificationPayloadInterface.ts delete mode 100644 src/modules/auth/strategies/google.strategy.ts diff --git a/src/modules/auth/auth.controller.ts b/src/modules/auth/auth.controller.ts index 06f60a2f1..f57a3294c 100644 --- a/src/modules/auth/auth.controller.ts +++ b/src/modules/auth/auth.controller.ts @@ -117,10 +117,10 @@ export default class RegistrationController { @ApiOperation({ summary: 'Google Authentication' }) @ApiBody({ type: GoogleAuthPayloadDto }) @ApiResponse({ status: 200, description: 'Verify Payload sent from google', type: AuthResponseDto }) - @ApiBadRequestResponse({ description: 'Invalid Google token' }) + @ApiBadRequestResponse({ description: 'Google authentication failed' }) @HttpCode(200) - async googleAuth(@Body() body: GoogleAuthPayload, @Query('mobile') isMobile: string) { - return this.authService.googleAuth({ googleAuthPayload: body, isMobile }); + async googleAuth(@Body() body: GoogleAuthPayload) { + return this.authService.googleAuth(body); } @skipAuth() diff --git a/src/modules/auth/auth.module.ts b/src/modules/auth/auth.module.ts index 8d4bb7657..ed8cb3972 100644 --- a/src/modules/auth/auth.module.ts +++ b/src/modules/auth/auth.module.ts @@ -13,14 +13,12 @@ import { EmailModule } from '../email/email.module'; import { OtpService } from '../otp/otp.service'; import { EmailService } from '../email/email.service'; import { Otp } from '../otp/entities/otp.entity'; -import { GoogleStrategy } from './strategies/google.strategy'; -import { GoogleAuthService } from './google-auth.service'; import { Profile } from '../profile/entities/profile.entity'; -import QueueService from '../email/queue.service'; import { OrganisationsService } from '../organisations/organisations.service'; import { Organisation } from '../organisations/entities/organisations.entity'; import { OrganisationUserRole } from '../role/entities/organisation-user-role.entity'; import { Role } from '../role/entities/role.entity'; +import { ProfileService } from '../profile/profile.service'; @Module({ controllers: [RegistrationController], @@ -30,9 +28,8 @@ import { Role } from '../role/entities/role.entity'; UserService, OtpService, EmailService, - GoogleStrategy, - GoogleAuthService, OrganisationsService, + ProfileService, ], imports: [ TypeOrmModule.forFeature([User, Otp, Profile, Organisation, OrganisationUserRole, Role]), diff --git a/src/modules/auth/auth.service.ts b/src/modules/auth/auth.service.ts index 63e474ce3..eb061157a 100644 --- a/src/modules/auth/auth.service.ts +++ b/src/modules/auth/auth.service.ts @@ -13,12 +13,13 @@ import { ForgotPasswordDto } from './dto/forgot-password.dto'; import { LoginDto } from './dto/login.dto'; import { RequestSigninTokenDto } from './dto/request-signin-token.dto'; import { OtpDto } from '../otp/dto/otp.dto'; -import { GoogleAuthService } from './google-auth.service'; import GoogleAuthPayload from './interfaces/GoogleAuthPayloadInterface'; -import { GoogleVerificationPayloadInterface } from './interfaces/GoogleVerificationPayloadInterface'; import { CustomHttpException } from '../../helpers/custom-http-filter'; import { UpdatePasswordDto } from './dto/updatePasswordDto'; +import { TokenPayload } from 'google-auth-library'; import { OrganisationsService } from '../organisations/organisations.service'; +import { ProfileService } from '../profile/profile.service'; +import { UpdateProfileDto } from '../profile/dto/update-profile.dto'; @Injectable() export default class AuthenticationService { @@ -27,8 +28,8 @@ export default class AuthenticationService { private jwtService: JwtService, private otpService: OtpService, private emailService: EmailService, - private googleAuthService: GoogleAuthService, - private organisationService: OrganisationsService + private organisationService: OrganisationsService, + private profileService: ProfileService ) {} async createNewUser(createUserDto: CreateUserDTO) { @@ -48,7 +49,7 @@ export default class AuthenticationService { if (!user) { throw new CustomHttpException(SYS_MSG.FAILED_TO_CREATE_USER, HttpStatus.BAD_REQUEST); } - const newOrganisationPaload = { + const newOrganisationPayload = { name: `${user.first_name}'s Organisation`, description: '', email: user.email, @@ -59,7 +60,7 @@ export default class AuthenticationService { state: '', }; - const newOrganisation = await this.organisationService.create(newOrganisationPaload, user.id); + const newOrganisation = await this.organisationService.create(newOrganisationPayload, user.id); const userOranisations = await this.organisationService.getAllUserOrganisations(user.id); const isSuperAdmin = userOranisations.map(instance => instance.user_role).includes('super-admin'); @@ -283,16 +284,22 @@ export default class AuthenticationService { }; } - async googleAuth({ googleAuthPayload, isMobile }: { googleAuthPayload: GoogleAuthPayload; isMobile: string }) { + async googleAuth(googleAuthPayload: GoogleAuthPayload) { const idToken = googleAuthPayload.id_token; - let verifyTokenResponse: GoogleVerificationPayloadInterface; - if (isMobile === 'true') { - const request = await fetch(`https://www.googleapis.com/oauth2/v3/tokeninfo?id_token=${idToken}`); - verifyTokenResponse = await request.json(); - } else { - verifyTokenResponse = await this.googleAuthService.verifyToken(idToken); + if (!idToken) { + throw new CustomHttpException(SYS_MSG.INVALID_CREDENTIALS, HttpStatus.BAD_REQUEST); + } + + const request = await fetch(`https://www.googleapis.com/oauth2/v3/tokeninfo?id_token=${idToken}`); + + if (request.status === 400) { + throw new CustomHttpException(SYS_MSG.INVALID_CREDENTIALS, HttpStatus.BAD_REQUEST); + } + if (request.status === 500) { + throw new CustomHttpException(SYS_MSG.SERVER_ERROR, HttpStatus.INTERNAL_SERVER_ERROR); } + const verifyTokenResponse: TokenPayload = await request.json(); const userEmail = verifyTokenResponse.email; const userExists = await this.userService.getUserRecord({ identifier: userEmail, identifierType: 'email' }); @@ -303,6 +310,7 @@ export default class AuthenticationService { first_name: verifyTokenResponse.given_name || '', last_name: verifyTokenResponse?.family_name || '', password: '', + profile_pic_url: verifyTokenResponse?.picture || '', }; return await this.createUserGoogle(userCreationPayload); } @@ -316,6 +324,12 @@ export default class AuthenticationService { first_name: userExists.first_name, last_name: userExists.last_name, }); + + if (userExists.profile.profile_pic_url === null) { + const updateDto = new UpdateProfileDto(); + updateDto.profile_pic_url = verifyTokenResponse?.picture; + await this.profileService.updateProfile(userExists.profile.id, updateDto); + } return { message: SYS_MSG.LOGIN_SUCCESSFUL, access_token: accessToken, @@ -358,9 +372,14 @@ export default class AuthenticationService { first_name: userPayload.first_name, last_name: userPayload.last_name, }); + if (userPayload.profile_pic_url) { + const updateDto = new UpdateProfileDto(); + updateDto.profile_pic_url = userPayload.profile_pic_url; + await this.profileService.updateProfile(newUser.profile.id, updateDto); + } return { - status_code: HttpStatus.OK, + status_code: HttpStatus.CREATED, message: SYS_MSG.USER_CREATED, access_token: accessToken, data: { @@ -370,6 +389,7 @@ export default class AuthenticationService { first_name: newUser.first_name, last_name: newUser.last_name, is_superadmin: isSuperAdmin, + avatar_url: newUser.profile.profile_pic_url, }, organisations: userOranisations, }, diff --git a/src/modules/auth/dto/create-user.dto.ts b/src/modules/auth/dto/create-user.dto.ts index f2aa5d85c..beaadc611 100644 --- a/src/modules/auth/dto/create-user.dto.ts +++ b/src/modules/auth/dto/create-user.dto.ts @@ -25,6 +25,15 @@ export class CreateUserDTO { @IsString() last_name: string; + @ApiProperty({ + description: 'The URL for the user profile picture', + example: 'https://example.com/profile-pic.jpg', + required: false, + }) + @IsOptional() + @IsString() + profile_pic_url?: string; + @ApiProperty({ description: 'The password for the user account.\ diff --git a/src/modules/auth/google-auth.service.ts b/src/modules/auth/google-auth.service.ts deleted file mode 100644 index 291d443c5..000000000 --- a/src/modules/auth/google-auth.service.ts +++ /dev/null @@ -1,25 +0,0 @@ -import { BadRequestException, Injectable } from '@nestjs/common'; -import authConfig from '../../../config/auth.config'; -import { OAuth2Client } from 'google-auth-library'; - -@Injectable() -export class GoogleAuthService { - private clientId = authConfig().google.clientID; - private client = new OAuth2Client(this.clientId); - - async verifyToken(token: string): Promise { - try { - const ticket = await this.client.verifyIdToken({ - idToken: token, - audience: this.clientId, - }); - - const payload = ticket.getPayload(); - - return payload; - } catch (error) { - console.log(error); - throw new BadRequestException('Invalid Google token'); - } - } -} diff --git a/src/modules/auth/interfaces/GoogleAuthPayloadInterface.ts b/src/modules/auth/interfaces/GoogleAuthPayloadInterface.ts index a475149a2..94dcd9386 100644 --- a/src/modules/auth/interfaces/GoogleAuthPayloadInterface.ts +++ b/src/modules/auth/interfaces/GoogleAuthPayloadInterface.ts @@ -1,21 +1,3 @@ export default interface GoogleAuthPayload { - access_token: string; - - expires_in: number; - - refresh_token: string; - - scope: string; - - token_type: string; - id_token: string; - - expires_at: number; - - provider: string; - - type: string; - - providerAccountId: string; } diff --git a/src/modules/auth/interfaces/GoogleVerificationPayloadInterface.ts b/src/modules/auth/interfaces/GoogleVerificationPayloadInterface.ts deleted file mode 100644 index e51e00e26..000000000 --- a/src/modules/auth/interfaces/GoogleVerificationPayloadInterface.ts +++ /dev/null @@ -1,27 +0,0 @@ -export interface GoogleVerificationPayloadInterface { - iss: string; - - azp: string; - - aud: string; - - sub: string; - - email: string; - - email_verified: true; - - at_hash: string; - - name: string; - - picture: string; - - given_name: string; - - family_name: string; - - iat: number; - - exp: number; -} diff --git a/src/modules/auth/strategies/google.strategy.ts b/src/modules/auth/strategies/google.strategy.ts deleted file mode 100644 index 1ad1b992a..000000000 --- a/src/modules/auth/strategies/google.strategy.ts +++ /dev/null @@ -1,48 +0,0 @@ -import { Inject, Injectable } from '@nestjs/common'; -import { ConfigType } from '@nestjs/config'; -import { PassportStrategy } from '@nestjs/passport'; -import { InjectRepository } from '@nestjs/typeorm'; -import { Repository } from 'typeorm'; -import config from '../../../../config/auth.config'; -import { User } from '../../user/entities/user.entity'; -import { Strategy, VerifyCallback } from 'passport-google-oauth20'; - -@Injectable() -export class GoogleStrategy extends PassportStrategy(Strategy, 'google') { - constructor(@InjectRepository(User) private userRepository: Repository) { - super({ - clientID: config().google.clientID, - clientSecret: config().google.clientSecret, - callbackURL: config().google.callbackURL, - scope: ['profile', 'email'], - }); - } - - async validate(accessToken: string, refreshToken: string, profile: any, done: VerifyCallback): Promise { - const { id, name, emails, photos } = profile; - - const user = { - first_name: name.givenName, - last_name: name.familyName, - email: emails[0].value, - password: '', - is_active: true, - secret: '', - is_2fa_enabled: false, - }; - try { - let existingUser = await this.userRepository.findOne({ - where: { email: user.email }, - }); - - if (!existingUser) { - existingUser = this.userRepository.create(user); - await this.userRepository.save(existingUser); - } - - return done(null, existingUser); - } catch (error) { - return done(error, false); - } - } -} From b6505e3b1fb2a357883cabfb9d6b2beea742f25b Mon Sep 17 00:00:00 2001 From: Prudent Bird Date: Sat, 24 Aug 2024 21:30:15 +0100 Subject: [PATCH 5/9] chore: fixed lint errors --- src/modules/auth/auth.service.ts | 6 +++--- src/modules/auth/tests/auth.service.spec.ts | 10 ---------- 2 files changed, 3 insertions(+), 13 deletions(-) diff --git a/src/modules/auth/auth.service.ts b/src/modules/auth/auth.service.ts index e918261c5..a9a2c6a2a 100644 --- a/src/modules/auth/auth.service.ts +++ b/src/modules/auth/auth.service.ts @@ -288,16 +288,16 @@ export default class AuthenticationService { const idToken = googleAuthPayload.id_token; if (!idToken) { - throw new CustomHttpException(SYS_MSG.INVALID_CREDENTIALS, HttpStatus.BAD_REQUEST); + throw new CustomHttpException(SYS_MSG.INVALID_CREDENTIALS, HttpStatus.UNAUTHORIZED); } const request = await fetch(`https://www.googleapis.com/oauth2/v3/tokeninfo?id_token=${idToken}`); if (request.status === 400) { - throw new CustomHttpException(SYS_MSG.INVALID_CREDENTIALS, HttpStatus.BAD_REQUEST); + throw new CustomHttpException(SYS_MSG.INVALID_CREDENTIALS, HttpStatus.UNAUTHORIZED); } if (request.status === 500) { - throw new CustomHttpException(SYS_MSG.SERVER_ERROR, HttpStatus.INTERNAL_SERVER_ERROR); + throw new CustomHttpException(SYS_MSG.SERVER_ERROR, HttpStatus.UNAUTHORIZED); } const verifyTokenResponse: TokenPayload = await request.json(); diff --git a/src/modules/auth/tests/auth.service.spec.ts b/src/modules/auth/tests/auth.service.spec.ts index 5cde81cc2..ff87c23a4 100644 --- a/src/modules/auth/tests/auth.service.spec.ts +++ b/src/modules/auth/tests/auth.service.spec.ts @@ -13,11 +13,9 @@ import { User, UserType } from '../../user/entities/user.entity'; import { Otp } from '../../otp/entities/otp.entity'; import UserResponseDTO from '../../user/dto/user-response.dto'; import { LoginDto } from '../dto/login.dto'; -import { GoogleAuthService } from '../google-auth.service'; import { Profile } from '../../profile/entities/profile.entity'; import { CustomHttpException } from '../../../helpers/custom-http-filter'; import { OrganisationsService } from '../../../modules/organisations/organisations.service'; -import { Organisation } from '../../../modules/organisations/entities/organisations.entity'; jest.mock('speakeasy'); @@ -27,7 +25,6 @@ describe('AuthenticationService', () => { let jwtServiceMock: jest.Mocked; let otpServiceMock: jest.Mocked; let emailServiceMock: jest.Mocked; - let googleAuthServiceMock: jest.Mocked; let organisationServiceMock: jest.Mocked; beforeEach(async () => { @@ -43,12 +40,6 @@ describe('AuthenticationService', () => { createUser: jest.fn(), }, }, - { - provide: GoogleAuthService, - useValue: { - verifyToken: jest.fn(), - }, - }, { provide: JwtService, useValue: { @@ -84,7 +75,6 @@ describe('AuthenticationService', () => { jwtServiceMock = module.get(JwtService) as jest.Mocked; otpServiceMock = module.get(OtpService) as jest.Mocked; emailServiceMock = module.get(EmailService) as jest.Mocked; - googleAuthServiceMock = module.get(GoogleAuthService) as jest.Mocked; organisationServiceMock = module.get(OrganisationsService) as jest.Mocked; }); From 622cf86f68bdf2b6f5690d4e872b221eb1572fb0 Mon Sep 17 00:00:00 2001 From: Prudent Bird Date: Sat, 24 Aug 2024 21:42:06 +0100 Subject: [PATCH 6/9] chore: fixed test errors --- src/modules/auth/auth.service.ts | 2 +- src/modules/auth/tests/auth.service.spec.ts | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/modules/auth/auth.service.ts b/src/modules/auth/auth.service.ts index a9a2c6a2a..40866f6b1 100644 --- a/src/modules/auth/auth.service.ts +++ b/src/modules/auth/auth.service.ts @@ -297,7 +297,7 @@ export default class AuthenticationService { throw new CustomHttpException(SYS_MSG.INVALID_CREDENTIALS, HttpStatus.UNAUTHORIZED); } if (request.status === 500) { - throw new CustomHttpException(SYS_MSG.SERVER_ERROR, HttpStatus.UNAUTHORIZED); + throw new CustomHttpException(SYS_MSG.SERVER_ERROR, HttpStatus.INTERNAL_SERVER_ERROR); } const verifyTokenResponse: TokenPayload = await request.json(); diff --git a/src/modules/auth/tests/auth.service.spec.ts b/src/modules/auth/tests/auth.service.spec.ts index ff87c23a4..a8590c04a 100644 --- a/src/modules/auth/tests/auth.service.spec.ts +++ b/src/modules/auth/tests/auth.service.spec.ts @@ -16,6 +16,7 @@ import { LoginDto } from '../dto/login.dto'; import { Profile } from '../../profile/entities/profile.entity'; import { CustomHttpException } from '../../../helpers/custom-http-filter'; import { OrganisationsService } from '../../../modules/organisations/organisations.service'; +import { ProfileService } from '../../profile/profile.service'; jest.mock('speakeasy'); @@ -31,7 +32,7 @@ describe('AuthenticationService', () => { const module: TestingModule = await Test.createTestingModule({ providers: [ AuthenticationService, - + ProfileService, { provide: UserService, useValue: { From d4d615fc2d5526622b32b1e11cdf931b1ca21a46 Mon Sep 17 00:00:00 2001 From: Prudent Bird Date: Sat, 24 Aug 2024 21:47:41 +0100 Subject: [PATCH 7/9] chore: fixed test errors --- src/modules/auth/tests/auth.service.spec.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/modules/auth/tests/auth.service.spec.ts b/src/modules/auth/tests/auth.service.spec.ts index a8590c04a..6401ed185 100644 --- a/src/modules/auth/tests/auth.service.spec.ts +++ b/src/modules/auth/tests/auth.service.spec.ts @@ -23,6 +23,7 @@ jest.mock('speakeasy'); describe('AuthenticationService', () => { let service: AuthenticationService; let userServiceMock: jest.Mocked; + let profileServiceMock: jest.Mocked; let jwtServiceMock: jest.Mocked; let otpServiceMock: jest.Mocked; let emailServiceMock: jest.Mocked; @@ -73,6 +74,7 @@ describe('AuthenticationService', () => { service = module.get(AuthenticationService); userServiceMock = module.get(UserService) as jest.Mocked; + profileServiceMock = module.get(ProfileService) as jest.Mocked; jwtServiceMock = module.get(JwtService) as jest.Mocked; otpServiceMock = module.get(OtpService) as jest.Mocked; emailServiceMock = module.get(EmailService) as jest.Mocked; From 543bf207826e7c2bb4ab000fc58056493eac98cc Mon Sep 17 00:00:00 2001 From: Prudent Bird Date: Sat, 24 Aug 2024 22:00:39 +0100 Subject: [PATCH 8/9] chore: fixed test errors --- src/modules/auth/tests/auth.service.spec.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/modules/auth/tests/auth.service.spec.ts b/src/modules/auth/tests/auth.service.spec.ts index 6401ed185..0e28eb19c 100644 --- a/src/modules/auth/tests/auth.service.spec.ts +++ b/src/modules/auth/tests/auth.service.spec.ts @@ -33,7 +33,6 @@ describe('AuthenticationService', () => { const module: TestingModule = await Test.createTestingModule({ providers: [ AuthenticationService, - ProfileService, { provide: UserService, useValue: { @@ -42,6 +41,12 @@ describe('AuthenticationService', () => { createUser: jest.fn(), }, }, + { + provide: ProfileService, + useValue: { + updateProfile: jest.fn(), + }, + }, { provide: JwtService, useValue: { From 5dc2f652b291460d0d4c8d273ece5e3204cff483 Mon Sep 17 00:00:00 2001 From: Prudent Bird Date: Sat, 24 Aug 2024 22:06:15 +0100 Subject: [PATCH 9/9] chore: added update to profile pic for social auth --- src/modules/auth/auth.service.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/modules/auth/auth.service.ts b/src/modules/auth/auth.service.ts index 40866f6b1..16303230a 100644 --- a/src/modules/auth/auth.service.ts +++ b/src/modules/auth/auth.service.ts @@ -325,11 +325,12 @@ export default class AuthenticationService { last_name: userExists.last_name, }); - if (userExists.profile.profile_pic_url === null) { + if (!userExists.profile.profile_pic_url || userExists.profile.profile_pic_url !== verifyTokenResponse.picture) { const updateDto = new UpdateProfileDto(); - updateDto.profile_pic_url = verifyTokenResponse?.picture; + updateDto.profile_pic_url = verifyTokenResponse.picture; await this.profileService.updateProfile(userExists.profile.id, updateDto); } + return { message: SYS_MSG.LOGIN_SUCCESSFUL, access_token: accessToken,