-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 🔥 support getting secrets api
support getting secrets api
- Loading branch information
tal-rofe
committed
Jul 29, 2022
1 parent
82dee40
commit 1feba40
Showing
10 changed files
with
91 additions
and
3 deletions.
There are no files selected for viewing
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
31 changes: 31 additions & 0 deletions
31
apps/backend/src/modules/user/modules/secrets/get-all.controller.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { Controller, Get, Logger } from '@nestjs/common'; | ||
import { QueryBus } from '@nestjs/cqrs'; | ||
|
||
import { CurrentUserId } from '@/decorators/current-user-id.decorator'; | ||
|
||
import Routes from './secrets.routes'; | ||
import type { IGetAllSecretsResponse } from './interfaces/responses'; | ||
import { GetAllSecretsContract } from './queries/contracts/get-all-secrets.contract'; | ||
import type { IUserSecretsGetAll } from './interfaces/user-secrets'; | ||
|
||
@Controller(Routes.CONTROLLER) | ||
export class GetAllController { | ||
private readonly logger = new Logger(GetAllController.name); | ||
|
||
constructor(private readonly queryBus: QueryBus) {} | ||
|
||
@Get(Routes.GET_ALL) | ||
public async getAll(@CurrentUserId() userId: string): Promise<IGetAllSecretsResponse> { | ||
this.logger.log(`Will try to fetch all secrets belong to use with an Id: "${userId}"`); | ||
|
||
const userSecrets = await this.queryBus.execute<GetAllSecretsContract, IUserSecretsGetAll[]>( | ||
new GetAllSecretsContract(userId), | ||
); | ||
|
||
this.logger.log(`Successfully got all secrets belong to user with an Id: "${userId}"`); | ||
|
||
return { | ||
secrets: userSecrets, | ||
}; | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
apps/backend/src/modules/user/modules/secrets/interfaces/responses.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,13 @@ | ||
import type { IUserSecretsGetAll } from './user-secrets'; | ||
|
||
export interface ICreateClientSecret { | ||
clientSecret: string; | ||
} | ||
|
||
export interface IRefreshClientSecret { | ||
clientSecret: string; | ||
} | ||
|
||
export interface IGetAllSecretsResponse { | ||
secrets: IUserSecretsGetAll[]; | ||
} |
6 changes: 6 additions & 0 deletions
6
apps/backend/src/modules/user/modules/secrets/interfaces/user-secrets.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export interface IUserSecretsGetAll { | ||
readonly id: string; | ||
readonly label: string; | ||
readonly expiration: number; | ||
readonly createdAt: number; | ||
} |
3 changes: 3 additions & 0 deletions
3
apps/backend/src/modules/user/modules/secrets/queries/contracts/get-all-secrets.contract.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export class GetAllSecretsContract { | ||
constructor(public readonly userId: string) {} | ||
} |
20 changes: 20 additions & 0 deletions
20
apps/backend/src/modules/user/modules/secrets/queries/handlers/get-all-secrets.handler.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { QueryHandler, type IQueryHandler } from '@nestjs/cqrs'; | ||
|
||
import { DBClientSecretService } from '@/modules/database/client-secret.service'; | ||
|
||
import { GetAllSecretsContract } from '../contracts/get-all-secrets.contract'; | ||
|
||
@QueryHandler(GetAllSecretsContract) | ||
export class GetAllSecretsHandler implements IQueryHandler<GetAllSecretsContract> { | ||
constructor(private readonly dbClientSecretsService: DBClientSecretService) {} | ||
|
||
async execute(contract: GetAllSecretsContract) { | ||
const secrets = await this.dbClientSecretsService.getSecrets(contract.userId); | ||
|
||
return secrets.map((secret) => ({ | ||
...secret, | ||
createdAt: secret.createdAt.getTime(), | ||
expiration: secret.expiration ? secret.expiration.getTime() : null, | ||
})); | ||
} | ||
} |
3 changes: 2 additions & 1 deletion
3
apps/backend/src/modules/user/modules/secrets/queries/handlers/index.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import { CreateSecretHandler } from './create-secret.handler'; | ||
import { GetAllSecretsHandler } from './get-all-secrets.handler'; | ||
import { RefreshSecretHandler } from './refresh-secret.handler'; | ||
|
||
export const QueryHandlers = [CreateSecretHandler, RefreshSecretHandler]; | ||
export const QueryHandlers = [CreateSecretHandler, RefreshSecretHandler, GetAllSecretsHandler]; |
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