Skip to content

Commit

Permalink
refactor: naming for approve use case and push notifications scenarios
Browse files Browse the repository at this point in the history
  • Loading branch information
Birloi Florian authored and Birloi Florian committed Sep 25, 2024
1 parent 1751952 commit eada2c4
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 83 deletions.
6 changes: 3 additions & 3 deletions backend/src/api/documents/document-contract.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
} from 'src/infrastructure/presenters/generic-paginated.presenter';
import { GetManyDocumentContractsDto } from './dto/get-many-document-contracts.dto';
import { UuidValidationPipe } from 'src/infrastructure/pipes/uuid.pipe';
import { ApproveDocumentContractByNgoUsecase } from 'src/usecases/documents/new_contracts/approve-document-contract-by-ngo.usecase';
import { ValidateDocumentContractByNgoUsecase } from 'src/usecases/documents/new_contracts/validate-document-contract-by-ngo.usecase';
import { SignDocumentContractByNgoUsecase } from 'src/usecases/documents/new_contracts/sign-document-contract-by-ngo.usecase';
import { RejectDocumentContractByNgoUsecase } from 'src/usecases/documents/new_contracts/reject-document-contract-by-ngo.usecase';
import { RejectDocumentContractByNgoDTO } from './dto/reject-document-contract.dto';
Expand All @@ -40,7 +40,7 @@ export class DocumentContractController {
constructor(
private readonly createDocumentContractUsecase: CreateDocumentContractUsecase,
private readonly getManyDocumentContractsUsecase: GetManyDocumentContractsUsecase,
private readonly approveDocumentContractByNgoUsecase: ApproveDocumentContractByNgoUsecase,
private readonly validateDocumentContractByNgoUsecase: ValidateDocumentContractByNgoUsecase,
private readonly rejectDocumentContractByNgoUsecase: RejectDocumentContractByNgoUsecase,
private readonly signDocumentContractByNGO: SignDocumentContractByNgoUsecase,
private readonly getOneDocumentContractForNgoUsecase: GetOneDocumentContractForNgoUsecase,
Expand Down Expand Up @@ -116,7 +116,7 @@ export class DocumentContractController {
@Param('id', UuidValidationPipe) id: string,
@ExtractUser() admin: IAdminUserModel,
): Promise<void> {
await this.approveDocumentContractByNgoUsecase.execute(id, admin);
await this.validateDocumentContractByNgoUsecase.execute(id, admin);
}

@Patch(':id/sign')
Expand Down
12 changes: 0 additions & 12 deletions backend/src/common/constants/notifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,18 +85,6 @@ export const NOTIFICATIONS = {
body: '',
},
},
APPROVE_CONTRACT: {
PUSH: {
title: 'VIC',
body: (organizationName: string): string =>
`Contractul tău cu ${organizationName} a fost aprobat și semnat`,
},
EMAIL: {
subject: (organizationName: string): string =>
`Contractul tău cu ${organizationName} a fost aprobat și semnat`,
body: '',
},
},
SIGN_CONTRACT_BY_NGO: {
PUSH: {
title: 'VIC',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,6 @@ export class DocumentContractRepositoryService extends RepositoryWithPagination<
): Promise<IDocumentContractModel> {
const documentContract = await this.documentContractRepository.findOne({
where: options,
relations: {
volunteer: {
user: {
notificationsSettings: true,
},
organization: true,
},
},
});

return DocumentContractTransformer.fromEntity(documentContract);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import BaseEvent from '../base-event.class';

export default class ApproveContractEvent extends BaseEvent {
export default class SignContractEvent extends BaseEvent {
constructor(
_organizationId: string,
_userId: string,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { NOTIFICATIONS } from 'src/common/constants/notifications';
import { PushNotificationsFacade } from '../notifications.facade';
import { MailService } from 'src/modules/mail/services/mail.service';
import GenerateContractEvent from '../events/documents/generate-contract.event';
import ApproveContractEvent from '../events/documents/approve-contract.event';
import RejectContractEvent from '../events/documents/reject-contract.event';
import SignContractEvent from '../events/documents/sign-contract.event';

@Injectable()
export class DocumentsListener {
Expand Down Expand Up @@ -57,7 +57,7 @@ export class DocumentsListener {
}

@OnEvent(EVENTS.DOCUMENTS.SIGN_CONTRACT_BY_NGO)
async onApproveContractEvent(payload: ApproveContractEvent): Promise<void> {
async onSignContractEvent(payload: SignContractEvent): Promise<void> {
const {
userId,
organizationId,
Expand All @@ -79,8 +79,8 @@ export class DocumentsListener {

this.pushNotificationsFacade.send({
userIds: [userId],
title: NOTIFICATIONS.APPROVE_CONTRACT.PUSH.title,
body: NOTIFICATIONS.APPROVE_CONTRACT.PUSH.body(organizationName),
title: NOTIFICATIONS.SIGN_CONTRACT_BY_NGO.PUSH.title,
body: NOTIFICATIONS.SIGN_CONTRACT_BY_NGO.PUSH.body(organizationName),
data: notificationData,
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { DocumentContractFacade } from 'src/modules/documents/services/document-
import { EVENTS } from 'src/modules/notifications/constants/events.constants';
import RejectContractEvent from 'src/modules/notifications/events/documents/reject-contract.event';
import { IAdminUserModel } from 'src/modules/user/models/admin-user.model';
import { VolunteerFacade } from 'src/modules/volunteer/services/volunteer.facade';

@Injectable()
export class RejectDocumentContractByNgoUsecase
Expand All @@ -21,6 +22,7 @@ export class RejectDocumentContractByNgoUsecase
private readonly exceptionService: ExceptionsService,
private readonly actionsArchiveFacade: ActionsArchiveFacade,
private readonly eventEmitter: EventEmitter2,
private readonly volunteerFacade: VolunteerFacade,
) {}

public async execute({
Expand Down Expand Up @@ -88,21 +90,24 @@ export class RejectDocumentContractByNgoUsecase
admin,
);

// get volunteer data to build the mail/notification subject/body
const volunteer = await this.volunteerFacade.find({
id: updatedContract.volunteerId,
});

// send push notifications and or email
this.eventEmitter.emit(
EVENTS.DOCUMENTS.REJECT_CONTRACT_BY_NGO,
new RejectContractEvent(
contract.organizationId,
contract.volunteer.user.id,
contract.volunteer.organization.name,
contract.volunteer.user.notificationsSettings.notificationsViaPush,
contract.volunteer.user.notificationsSettings.notificationsViaEmail,
contract.volunteer.user.email,
volunteer.user.id,
volunteer.organization.name,
volunteer.user.notificationsSettings.notificationsViaPush,
volunteer.user.notificationsSettings.notificationsViaEmail,
volunteer.user.email,
contract.id,
rejectionReason || '',
),
);

console.log(rejectionReason);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ import { ActionsArchiveFacade } from 'src/modules/actions-archive/actions-archiv
import { TrackedEventName } from 'src/modules/actions-archive/enums/action-resource-types.enum';
import { DocumentContractStatus } from 'src/modules/documents/enums/contract-status.enum';
import { ContractExceptionMessages } from 'src/modules/documents/exceptions/contract.exceptions';
import { IDocumentContractModel } from 'src/modules/documents/models/document-contract.model';
import { DocumentContractFacade } from 'src/modules/documents/services/document-contract.facade';
import { EVENTS } from 'src/modules/notifications/constants/events.constants';
import ApproveContractEvent from 'src/modules/notifications/events/documents/approve-contract.event';
import { DocumentSignatureFacade } from 'src/modules/documents/services/document-signature.facade';
import { DocumentPDFGenerator } from 'src/modules/documents/services/document-pdf-generator';
import { IAdminUserModel } from 'src/modules/user/models/admin-user.model';
import { VolunteerFacade } from 'src/modules/volunteer/services/volunteer.facade';
import { IDocumentContractModel } from 'src/modules/documents/models/document-contract.model';
import SignContractEvent from 'src/modules/notifications/events/documents/sign-contract.event';

@Injectable()
export class SignDocumentContractByNgoUsecase implements IUseCaseService<void> {
Expand All @@ -23,6 +24,7 @@ export class SignDocumentContractByNgoUsecase implements IUseCaseService<void> {
private readonly actionsArchiveFacade: ActionsArchiveFacade,
private readonly eventEmitter: EventEmitter2,
private readonly documentPDFGenerator: DocumentPDFGenerator,
private readonly volunteerFacade: VolunteerFacade,
) {}

public async execute(
Expand Down Expand Up @@ -63,22 +65,8 @@ export class SignDocumentContractByNgoUsecase implements IUseCaseService<void> {

this.documentPDFGenerator.generateContractPDF(documentContractId);

// send push notifications and or email
this.eventEmitter.emit(
EVENTS.DOCUMENTS.SIGN_CONTRACT_BY_NGO,
new ApproveContractEvent(
contract.organizationId,
contract.volunteerId,
contract.volunteer.organization.name,
contract.volunteer.user.notificationsSettings.notificationsViaPush,
contract.volunteer.user.notificationsSettings.notificationsViaEmail,
contract.volunteer.user.email,
contract.id,
),
);

// Sign Document Contract by NGO
await this.actionsArchiveFacade.trackEvent(
this.actionsArchiveFacade.trackEvent(
TrackedEventName.SIGN_DOCUMENT_CONTRACT_BY_NGO,
{
organizationId: admin.organizationId,
Expand All @@ -89,5 +77,24 @@ export class SignDocumentContractByNgoUsecase implements IUseCaseService<void> {
},
admin,
);

// get volunteer data to build the mail/notification subject/body
const volunteer = await this.volunteerFacade.find({
id: contract.volunteerId,
});

// send push notifications and or email
this.eventEmitter.emit(
EVENTS.DOCUMENTS.SIGN_CONTRACT_BY_NGO,
new SignContractEvent(
contract.organizationId,
volunteer.user.id,
volunteer.organization.name,
volunteer.user.notificationsSettings.notificationsViaPush,
volunteer.user.notificationsSettings.notificationsViaEmail,
volunteer.user.email,
contract.id,
),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { DocumentContractFacade } from 'src/modules/documents/services/document-
import { IAdminUserModel } from 'src/modules/user/models/admin-user.model';

@Injectable()
export class ApproveDocumentContractByNgoUsecase {
export class ValidateDocumentContractByNgoUsecase {
constructor(
private readonly documentContractFacade: DocumentContractFacade,
private readonly exceptionService: ExceptionsService,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ import { TrackedEventName } from 'src/modules/actions-archive/enums/action-resou
import { GetOrganizationUseCaseService } from '../organization/get-organization.usecase';
import { IAdminUserModel } from 'src/modules/user/models/admin-user.model';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { EVENTS } from 'src/modules/notifications/constants/events.constants';
import ApproveContractEvent from 'src/modules/notifications/events/documents/approve-contract.event';

@Injectable()
export class SignAndConfirmContractUsecase
Expand Down Expand Up @@ -67,20 +65,6 @@ export class SignAndConfirmContractUsecase
status: ContractStatus.APPROVED,
});

// send push notifications and or email
this.eventEmitter.emit(
EVENTS.DOCUMENTS.APPROVE_CONTRACT,
new ApproveContractEvent(
organization.id,
contract.volunteer.user.id,
organization.name,
contract.volunteer.user.notificationsSettings.notificationsViaPush,
contract.volunteer.user.notificationsSettings.notificationsViaEmail,
contract.volunteer.user.email,
contract.id,
),
);

// Track event
this.actionsArchiveFacade.trackEvent(
TrackedEventName.APPROVE_CONTRACT,
Expand Down
4 changes: 2 additions & 2 deletions backend/src/usecases/use-case.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ import { SignDocumentContractByVolunteerUsecase } from './documents/new_contract
import { RejectDocumentContractByVolunteerUsecase } from './documents/new_contracts/reject-document-contact-by-volunteer.usecase';
import { GetManyDocumentContractsByVolunteerUsecase } from './documents/new_contracts/get-many-document-contracts-by-volunteer.usecase';
import { GetOneDocumentContractForVolunteerUsecase } from './documents/new_contracts/get-one-document-contract-for-volunteer.usecase';
import { ApproveDocumentContractByNgoUsecase } from './documents/new_contracts/approve-document-contract-by-ngo.usecase';
import { ValidateDocumentContractByNgoUsecase } from './documents/new_contracts/validate-document-contract-by-ngo.usecase';
import { SignDocumentContractByNgoUsecase } from './documents/new_contracts/sign-document-contract-by-ngo.usecase';
import { DeleteDocumentTemplateUsecase } from './documents/new_contracts/delete-document-template.usecase';
import { RejectDocumentContractByNgoUsecase } from './documents/new_contracts/reject-document-contract-by-ngo.usecase';
Expand Down Expand Up @@ -308,7 +308,7 @@ const providers = [
RejectDocumentContractByVolunteerUsecase,
GetManyDocumentContractsByVolunteerUsecase,
GetOneDocumentContractForVolunteerUsecase,
ApproveDocumentContractByNgoUsecase,
ValidateDocumentContractByNgoUsecase,
SignDocumentContractByNgoUsecase,
RejectDocumentContractByNgoUsecase,
GetOneDocumentContractForNgoUsecase,
Expand Down
21 changes: 9 additions & 12 deletions mobile/src/contexts/notification/NotificationContextProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,8 @@ export const EVENTS = {
},
DOCUMENTS: {
GENERATE_CONTRACT: 'contract.generate',
APPROVE_CONTRACT: 'contract.approve',
REJECT_CONATRCT: 'contract.reject',

SIGN_CONTRACT_BY_NGO: 'contract.sign.by.ngo',
REJECT_CONTRACT_BY_NGO: 'contract.reject.by.ngo',
SIGN_CONTRACT_BY_NGO: 'contract.sign.ngo',
REJECT_CONTRACT_BY_NGO: 'contract.reject.ngo',
},
};

Expand Down Expand Up @@ -120,14 +117,14 @@ const NotificationContextProvider = ({
}

if (
payload.key ===
(EVENTS.DOCUMENTS.GENERATE_CONTRACT ||
EVENTS.DOCUMENTS.APPROVE_CONTRACT ||
EVENTS.DOCUMENTS.REJECT_CONATRCT ||
EVENTS.DOCUMENTS.SIGN_CONTRACT_BY_NGO ||
EVENTS.DOCUMENTS.REJECT_CONTRACT_BY_NGO)
[
EVENTS.DOCUMENTS.GENERATE_CONTRACT,
EVENTS.DOCUMENTS.SIGN_CONTRACT_BY_NGO,
EVENTS.DOCUMENTS.REJECT_CONTRACT_BY_NGO,
].includes(payload.key)
) {
navigation.navigate('contract', {
// TBD: where do we navigate?
navigation.navigate('documents/contracts', {
id: payload.payload.contractId,
});
}
Expand Down

0 comments on commit eada2c4

Please sign in to comment.