Skip to content

Commit

Permalink
Add the possibility to call a custom method before sending usercard (o…
Browse files Browse the repository at this point in the history
…pfab#7902)

Signed-off-by: freddidierRTE <[email protected]>
  • Loading branch information
freddidierRTE authored and ClementBouvierN committed Feb 14, 2025
1 parent 4225125 commit d80e01d
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/docs/asciidoc/reference_doc/ui_api.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,28 @@ The template can receive the emitter entity of the card by registering a listene
opfab.currentUserCard.listenToEntityUsedForSendingCard((entityId) => {// do some stuff with the entity id})
```

=== Function registerFunctionToBeCalledBeforeCardSending

Before sending the card and after the preview, you can define a method to be called. This method is blocking (i.e., it is an async method awaited by the opfab code).

The card to be sent is provided as a parameter.


If the method throws an error, the error message will be shown to the user on the top screen banner and the card will not be sent.

For example :

```
opfab.currentCard.registerFunctionToBeCalledBeforeCardSending( async(cardToBeSent) => {

// do some stuff

// throws error if needed

})

```

=== Function registerFunctionToGetSpecificCardInformation

Register the template function to call to get user card specific information. This function will be called by opfab when user clicks on the "preview" button. More explanation can be found in the <<user_cards, user card chapter>>.
Expand Down
3 changes: 3 additions & 0 deletions ui/main/src/app/api/currentusercard.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ export class CurrentUserCardAPI {
registerFunctionToGetSpecificCardInformation: (getSpecificCardInformation) =>
UserCardTemplateGateway.setFunctionToGetSpecificCardInformationFromTemplate(getSpecificCardInformation),

registerFunctionToBeCalledBeforeCardSending: (beforeCardSending) =>
UserCardTemplateGateway.setFunctionToBeCalledBeforeCardSending(beforeCardSending),

setDropdownEntityRecipientList: (recipients) =>
UserCardTemplateGateway.setDropdownEntityRecipientList(recipients),

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ export class UserCardTemplateGateway {
private static _functionToGetSpecficCardInformationFromTemplate: Function;
private static _functionToSendEntityUsedForSendingCardToTemplate: Function;

private static _functionToBeCalledBeforeCardSending: Function;

public static init() {
UserCardTemplateGateway._editionMode = null;
UserCardTemplateGateway._endDate = null;
Expand All @@ -53,6 +55,7 @@ export class UserCardTemplateGateway {
UserCardTemplateGateway._functionToSetInitialSelectedRecipientsForInformation = () => {};
UserCardTemplateGateway._functionToSetSelectedRecipients = () => {};
UserCardTemplateGateway._functionToSetSelectedRecipientsForInformation = () => {};
UserCardTemplateGateway._functionToBeCalledBeforeCardSending = () => {};
}

public static initTemplateFunctions() {
Expand Down Expand Up @@ -142,6 +145,14 @@ export class UserCardTemplateGateway {
UserCardTemplateGateway._expirationDate = value;
}

public static setFunctionToBeCalledBeforeCardSending(functionToBeCalledBeforeCardSending: Function) {
UserCardTemplateGateway._functionToBeCalledBeforeCardSending = functionToBeCalledBeforeCardSending;
}

public static async callFunctionToBeCalledBeforeCardSending(cardToBeSent: any) {
await UserCardTemplateGateway._functionToBeCalledBeforeCardSending(cardToBeSent);
}

public static setFunctionToGetSpecificCardInformationFromTemplate(
functionToGetSpecficCardInformationFromTemplate: Function
) {
Expand Down
33 changes: 33 additions & 0 deletions ui/main/src/app/views/userCard/cardSender/cardSender.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ import {CardCreationReportData} from '@ofServices/cards/model/CardCreationReport
import {MessageLevel} from '@ofServices/alerteMessage/model/Message';
import {ServerResponse, ServerResponseStatus} from 'app/server/ServerResponse';
import {NotificationDecision} from 'app/services/notifications/NotificationDecision';
import {UserCardTemplateGateway} from '@ofServices/templateGateway/UserCardTemplateGateway';
import {CurrentUserCardAPI} from 'app/api/currentusercard.api';

declare const opfab: any;

describe('UserCard CardSender', () => {
let cardsServerMock: CardsServerMock;
Expand All @@ -25,6 +29,8 @@ describe('UserCard CardSender', () => {
cardsServerMock = new CardsServerMock();
CardsService.setCardsServer(cardsServerMock);
cardSender = new CardSender();
CurrentUserCardAPI.init();
UserCardTemplateGateway.init();
});
describe('Send a card', () => {
it('Should send to the back end', async () => {
Expand Down Expand Up @@ -58,6 +64,33 @@ describe('UserCard CardSender', () => {
await cardSender.sendCardAndChildCard(card);
expect(NotificationDecision.hasSentCard(card.process + '.' + card.processInstanceId)).toBeTrue();
});

it('Should call the function registered via api.currentUserCard.registerFunctionToBeCalledBeforeCardSending before sending the card', async () => {
let cardSendToMethod = undefined;
opfab.currentUserCard.registerFunctionToBeCalledBeforeCardSending(async (cardToBeSent) => {
cardSendToMethod = cardToBeSent;
});
await cardSender.sendCardAndChildCard(card);
expect(cardSendToMethod).toEqual(convertCardToCardForPublishing(card));
expect(cardsServerMock.cardsPosted.length).toBe(1);
});

it('Should not send the card if the function registered via api.currentUserCard.registerFunctionToBeCalledBeforeCardSending throws an error ', async () => {
opfab.currentUserCard.registerFunctionToBeCalledBeforeCardSending(async () => {
throw new Error('Error in function');
});
await cardSender.sendCardAndChildCard(card);
expect(cardsServerMock.cardsPosted.length).toBe(0);
});
it('Shoud display the error message if the function registered via api.currentUserCard.registerFunctionToBeCalledBeforeCardSending throws an error', async () => {
opfab.currentUserCard.registerFunctionToBeCalledBeforeCardSending(async () => {
throw new Error('Error in function');
});
const alertMessageReceiver = new AlertMessageReceiver();
await cardSender.sendCardAndChildCard(card);
const alertMessage = await alertMessageReceiver.getMessageReceived();
expect(alertMessage.message).toEqual('Error in function');
});
});
describe('send a card with a child card', () => {
const childCard = getOneCard();
Expand Down
9 changes: 9 additions & 0 deletions ui/main/src/app/views/userCard/cardSender/cardSender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {CardsService} from '@ofServices/cards/CardsService';
import {firstValueFrom} from 'rxjs';
import {LoggerService as logger} from 'app/services/logs/LoggerService';
import {NotificationDecision} from 'app/services/notifications/NotificationDecision';
import {UserCardTemplateGateway} from '@ofServices/templateGateway/UserCardTemplateGateway';

export class CardSender {
public async sendCardAndChildCard(card: Card, childCard?: Card, setCurrentDateForStartDate = false) {
Expand All @@ -28,6 +29,14 @@ export class CardSender {
startDate: new Date().valueOf()
};
}
try {
await UserCardTemplateGateway.callFunctionToBeCalledBeforeCardSending(cardForPublish);
} catch (error) {
AlertMessageService.sendAlertMessage(error);
logger.error(`Error while calling the function to be called before sending the card, error = ${error}`);
return;
}

NotificationDecision.addSentCard(card.process + '.' + card.processInstanceId);
const responseFromCardPost = await firstValueFrom(CardsService.postCard(cardForPublish));
if (responseFromCardPost.status !== ServerResponseStatus.OK) {
Expand Down

0 comments on commit d80e01d

Please sign in to comment.