Revoke / Terminate agreements

Any issue about Cyclos 4 scripting and Webservices

Moderators: rmvanarkel, hugo, alexandre

Post Reply
jakob.schumann
Posts: 20
Joined: Thu Apr 23, 2020 5:37 pm

Revoke / Terminate agreements

Post by jakob.schumann »

Hello!

How can I revoke an accepted agreement for an user via script? (e.g. when the agreement or some data changed)

a) for optional agreements, e.g. reset the SEPA mandate agreement when a new IBAN is entered
I found https://documentation.cyclos.org/curren ... ementsDTO) but how do I use it?
I create an UserOptionalAgreementsDTO and fill it with an AgreementVO for the agreement in question, but how do I tell the AgreementVO that it is not accepted?
Is it the correct method for this?

Or do I have to manually construct an AgreementLogVO and save this?

b) for required agreements, to force the user to accept it again on the next login, e.g. when the terms&conditions changed

Additionally: How can I show the user a message that he has to re-accept the agreement again after he changed his profile (script running on extension point User > Update)?

Code: Select all

alertService.create(user, UserAlertType.CUSTOM, "Test")
only notifies the admins.

Code: Select all

notificationHandler.notify(user, PersonalNotificationType.USER_STATUS_CHANGED, null, null, null, "Test")
only supports predefinied notifications.
Easiest would be an alert box like the "Saved successfully" message after clicking "Save", but a real custom notification through the notification center would be preferable.

Thanks in advance,
J.Schumann
jakob.schumann
Posts: 20
Joined: Thu Apr 23, 2020 5:37 pm

Re: Revoke / Terminate agreements

Post by jakob.schumann »

To answer myself (partly):

Agreements can be revoked by using the user's setAcceptedAgreements() method:

Code: Select all

import org.cyclos.entities.access.Agreement

def sepaAgreement = 
    entityManagerHandler.find(Agreement, scriptParameters.agreementName)

// get the user's agreements, we need them all to set them again without
// the SEPA agreement if that is checked
def currentAgreements = user.getAcceptedAgreements()

if (currentAgreements.contains(sepaAgreement)) {
    // revoke the agreement by only setting the remaining as accepted
    currentAgreements.remove(sepaAgreement)
    user.setAcceptedAgreements(currentAgreements)
}
As mentioned in the first post, Cyclos seems to only support predefined notifications but offers the possibility to modify those notifications with a script that can be set in System > Configurations > [your config] > Notifications > Notification Script:

Create a notification from your script using one of the predefined constants but giving an unusual/unexpected entity als parameter, so we can detect in the notification script which notif. should be customized:

Code: Select all

import org.cyclos.model.messaging.notifications.PersonalNotificationType

// the USER_STATUS_CHANGED is predefined, it has to be customized by a
// notification script, for it to be able to detect that this notification
// comes from us we supply the Agreement as entity parameter, the given
// message is irrelevant and won't be displayed by the notification script
notificationHandler.notify(
    user,
    PersonalNotificationType.USER_STATUS_CHANGED,
    null,
    sepaAgreement, // <- custom entity
    null,
    "irrelevant text, only visible when the notification script is not set"
)
The notification script checks for type + entity and then replaces title + body with our own content:

Code: Select all

import org.cyclos.model.messaging.notifications.PersonalNotificationType

if (
    type == PersonalNotificationType.USER_STATUS_CHANGED
    && entity instanceof org.cyclos.entities.access.Agreement
) {
    def className = entity.getClass()
    return [
        title: "your title",
        body: "your message"
    ]
}

// default values for the rest of the notification types
return null
Remaining question: Is this the best way to get custom notifications? Is it possible to add a message box like the "Saved successfully" message appearing after the profile form was submitted?

Best regards,
Jakob
Post Reply