-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move services and OtpRepository (#175)
- Loading branch information
1 parent
777edb3
commit cf13de5
Showing
17 changed files
with
179 additions
and
105 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains 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 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 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 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 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,26 @@ | ||
/* eslint-disable class-methods-use-this, lines-between-class-members, no-dupe-class-members */ | ||
import { injectable } from 'inversify' | ||
import { StorableOtp } from '../repositories/types' | ||
import { TwoWayMapper } from './TwoWayMapper' | ||
|
||
@injectable() | ||
export class OtpMapper implements TwoWayMapper<StorableOtp, string> { | ||
persistenceToDto(otp: string): StorableOtp | ||
persistenceToDto(otp: string | null): StorableOtp | null { | ||
if (!otp) { | ||
return null | ||
} | ||
return JSON.parse(otp) | ||
} | ||
|
||
dtoToPersistence(otp: StorableOtp): string | ||
dtoToPersistence(otp: StorableOtp | null): string | null { | ||
if (!otp) { | ||
return null | ||
} | ||
|
||
return JSON.stringify(otp) | ||
} | ||
} | ||
|
||
export default OtpMapper |
This file contains 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,7 @@ | ||
import { Mapper } from './Mapper' | ||
|
||
export interface TwoWayMapper<Dto, Persistence> | ||
extends Mapper<Dto, Persistence> { | ||
dtoToPersistence(persistence: Dto): Persistence | ||
dtoToPersistence(persistence: Dto | null): Persistence | null | ||
} |
This file contains 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,77 @@ | ||
/* eslint-disable class-methods-use-this */ | ||
|
||
import { inject, injectable } from 'inversify' | ||
import { otpClient } from '../redis' | ||
import { StorableOtp } from './types' | ||
import { otpExpiry } from '../config' | ||
import { OtpRepositoryInterface } from './interfaces/OtpRepositoryInterface' | ||
import { TwoWayMapper } from '../mappers/TwoWayMapper' | ||
import { DependencyIds } from '../constants' | ||
|
||
@injectable() | ||
export class OtpRepository implements OtpRepositoryInterface { | ||
private otpMapper: TwoWayMapper<StorableOtp, string> | ||
|
||
public constructor( | ||
@inject(DependencyIds.otpMapper) | ||
otpMapper: TwoWayMapper<StorableOtp, string>, | ||
) { | ||
this.otpMapper = otpMapper | ||
} | ||
|
||
public deleteOtpByEmail: (email: string) => Promise<void> = (email) => { | ||
return new Promise((resolve, reject) => { | ||
otpClient.del(email, (redisDelError, resdisDelResponse) => { | ||
if (redisDelError || resdisDelResponse !== 1) { | ||
reject(redisDelError) | ||
return | ||
} | ||
|
||
resolve() | ||
}) | ||
}) | ||
} | ||
|
||
public setOtpForEmail: (email: string, otp: StorableOtp) => Promise<void> = ( | ||
email, | ||
otp, | ||
) => { | ||
return new Promise((resolve, reject) => { | ||
otpClient.set( | ||
email, | ||
this.otpMapper.dtoToPersistence(otp), | ||
'EX', | ||
otpExpiry, | ||
(redisSetError) => { | ||
if (redisSetError) { | ||
reject(redisSetError) | ||
return | ||
} | ||
|
||
resolve() | ||
}, | ||
) | ||
}) | ||
} | ||
|
||
public getOtpForEmail: (email: string) => Promise<StorableOtp | null> = ( | ||
email, | ||
) => { | ||
return new Promise((resolve, reject) => { | ||
otpClient.get(email, (redisError, string) => { | ||
if (redisError) { | ||
reject(redisError) | ||
return | ||
} | ||
|
||
if (!string) { | ||
resolve(null) | ||
} | ||
|
||
resolve(this.otpMapper.persistenceToDto(string)) | ||
}) | ||
}) | ||
} | ||
} | ||
|
||
export default OtpRepository |
This file contains 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,9 @@ | ||
import { StorableOtp } from '../types' | ||
|
||
export interface OtpRepositoryInterface { | ||
deleteOtpByEmail(email: string): Promise<void> | ||
|
||
setOtpForEmail(email: string, otp: StorableOtp): Promise<void> | ||
|
||
getOtpForEmail(email: string): Promise<StorableOtp | null> | ||
} |
This file contains 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
4 changes: 2 additions & 2 deletions
4
src/server/api/analytics/analyticsLogger.ts → src/server/services/analyticsLogger.ts
This file contains 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 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
Oops, something went wrong.