diff --git a/.github/workflows/google-cloudrun-docker.yml b/.github/workflows/google-cloudrun-docker.yml index 7d339a4..3bc870c 100644 --- a/.github/workflows/google-cloudrun-docker.yml +++ b/.github/workflows/google-cloudrun-docker.yml @@ -65,20 +65,20 @@ jobs: - name: Checkout uses: actions/checkout@v2 - # - name: Google Auth - # id: auth - # uses: 'google-github-actions/auth@v0' - # with: - # token_format: 'access_token' - # workload_identity_provider: '${{ secrets.WIF_PROVIDER }}' # e.g. - projects/123456789/locations/global/workloadIdentityPools/my-pool/providers/my-provider - # service_account: '${{ secrets.WIF_SERVICE_ACCOUNT }}' # e.g. - my-service-account@my-project.iam.gserviceaccount.com - - # NOTE: Alternative option - authentication via credentials json - name: Google Auth id: auth uses: 'google-github-actions/auth@v0' with: - credentials_json: '${{ secrets.GCP_CREDENTIALS }}' + token_format: 'access_token' + workload_identity_provider: '${{ secrets.WIF_PROVIDER }}' # e.g. - projects/123456789/locations/global/workloadIdentityPools/my-pool/providers/my-provider + service_account: '${{ secrets.WIF_SERVICE_ACCOUNT }}' # e.g. - my-service-account@my-project.iam.gserviceaccount.com + + # NOTE: Alternative option - authentication via credentials json + # - name: Google Auth + # id: auth + # uses: 'google-github-actions/auth@v0' + # with: + # credentials_json: '${{ secrets.GCP_CREDENTIALS }}' # BEGIN - Docker auth and build (NOTE: If you already have a container image, these Docker steps can be omitted) diff --git a/src/modules/clients/data/typeorm/repositories/TypeOrmClientsRepository.ts b/src/modules/clients/data/typeorm/repositories/TypeOrmClientsRepository.ts index 496024f..1a34e99 100644 --- a/src/modules/clients/data/typeorm/repositories/TypeOrmClientsRepository.ts +++ b/src/modules/clients/data/typeorm/repositories/TypeOrmClientsRepository.ts @@ -10,6 +10,17 @@ export class TypeOrmClientsRepository implements ClientRepository { private readonly clientsRepository: Repository, ) {} + getTokenFromClient(clientId: string): Promise { + const token = this.clientsRepository.query( + ` + select fcm_token from fcm_tokens where client_id = $1 + `, + [clientId], + ); + + return token; + } + async addTokenToClient(clientId: string, token: string): Promise { const clientToken = await this.clientsRepository.query( ` diff --git a/src/modules/clients/domain/repositories/ClientRepository.ts b/src/modules/clients/domain/repositories/ClientRepository.ts index 40b8256..ff3b696 100644 --- a/src/modules/clients/domain/repositories/ClientRepository.ts +++ b/src/modules/clients/domain/repositories/ClientRepository.ts @@ -58,6 +58,7 @@ export interface ClientRepository { }): Promise; addTokenToClient(clientId: string, token: string): Promise; + getTokenFromClient(clientId: string): Promise; } export const ClientRepository = Symbol('ClientRepository'); diff --git a/src/modules/desk/desk.module.ts b/src/modules/desk/desk.module.ts index 220113b..78b456f 100644 --- a/src/modules/desk/desk.module.ts +++ b/src/modules/desk/desk.module.ts @@ -19,12 +19,14 @@ import { UpdateDeskUsecase } from './interactors/usecases/UpdateDeskUsecase'; import { CallNextClientOfDeskUsecase } from './interactors/usecases/CallNextClientOfDeskUsecase'; import { ServicesModule } from '../services/services.module'; import { QueuesModule } from '../queues/queues.module'; +import { ClientsModule } from '../clients/clients.module'; @Module({ imports: [ CommonModule, forwardRef(() => ServicesModule), forwardRef(() => QueuesModule), + forwardRef(() => ClientsModule), TypeOrmModule.forFeature([Desk]), ], controllers: [DeskController], diff --git a/src/modules/desk/interactors/usecases/CallNextClientOfDeskUsecase.ts b/src/modules/desk/interactors/usecases/CallNextClientOfDeskUsecase.ts index 79c9a6e..2e59d7d 100644 --- a/src/modules/desk/interactors/usecases/CallNextClientOfDeskUsecase.ts +++ b/src/modules/desk/interactors/usecases/CallNextClientOfDeskUsecase.ts @@ -9,6 +9,11 @@ import { QueueRepository } from '../../../queues/domain/repositories/QueueReposi import { QueueEntity } from '../../../queues/domain/entities/Queue.entity'; import { DeskWithCalledClient } from '../../domain/entities/Desk.entity'; import { DeskRepository } from '../../domain/repositories/DeskRepository'; +import { + NotificationService, + NotificationTypes, +} from '../../../common/domain/services/NotificationService'; +import { ClientRepository } from '../../../clients/domain/repositories/ClientRepository'; @Injectable() export class CallNextClientOfDeskUsecase implements UseCase { @@ -21,6 +26,12 @@ export class CallNextClientOfDeskUsecase implements UseCase { @Inject(QueueRepository) private queueRepository: QueueRepository, + + @Inject(NotificationService) + private notificationService: NotificationService, + + @Inject(ClientRepository) + private clientRepository: ClientRepository, ) {} async execute( deskId: string, @@ -54,6 +65,26 @@ export class CallNextClientOfDeskUsecase implements UseCase { ); // TODO: notify queue subscriber + this.notificationService.sendNotification( + client.queueId, + 'A fila andou', + 'A fila andou', + NotificationTypes.TO_TOPIC, + ); + + // TODO: notify client when he is called + const tokenFromClient = await this.clientRepository.getTokenFromClient( + client.id, + ); + + if (tokenFromClient) { + this.notificationService.sendNotification( + tokenFromClient, + 'VocĂȘ foi chamado', + 'VocĂȘ foi chamado', + NotificationTypes.TO_TOKEN, + ); + } const desk = await this.deskRepository.findById(deskId);