All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.venky.swf.plugins.templates.extensions.BeforeSaveUser Maven / Gradle / Ivy

The newest version!
package com.venky.swf.plugins.templates.extensions;

import com.venky.core.util.ObjectUtil;
import com.venky.swf.db.extensions.BeforeModelSaveExtension;
import com.venky.swf.integration.api.Call;
import com.venky.swf.integration.api.HttpMethod;
import com.venky.swf.integration.api.InputFormat;
import com.venky.swf.plugins.templates.db.model.User;
import com.venky.swf.routing.Config;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

import java.net.URLEncoder;
import java.util.HashSet;
import java.util.Set;

public class BeforeSaveUser extends BeforeModelSaveExtension {
    static {
        registerExtension(new BeforeSaveUser());
    }
    @Override
    public void beforeSave(User user) {
        if (ObjectUtil.isVoid(user.getPhoneNumber())) {
            return;
        }
        
        if (user.getRawRecord().isNewRecord() && !user.isWhatsAppNotificationEnabled()) {
            return;
        }

        if (user.getRawRecord().isFieldDirty("WHATS_APP_NOTIFICATION_ENABLED")){
            JSONObject input = new JSONObject();
            input.put("userid", Config.instance().getProperty("whatsapp.userid"));
            input.put("password",Config.instance().getProperty("whatsapp.password"));
            if (ObjectUtil.isVoid(input.get("userid")) || ObjectUtil.isVoid(input.get("password"))){
                return;
            }
            String whatsAppProviderUrl = Config.instance().getProperty("whatsapp.url");
            input.put("format","json");
            input.put("phone_number",user.getPhoneNumber().substring(1));
            input.put("v",1.1);
            input.put("auth_scheme","plain");
            input.put("channel","WHATSAPP");
            if (user.isWhatsAppNotificationEnabled()){
                input.put("method","OPT_IN");
            }else {
                input.put("method","OPT_OUT");
            }
            Call call = new Call().url(whatsAppProviderUrl).inputFormat(InputFormat.FORM_FIELDS).
                    input(input).method(HttpMethod.GET);
            JSONObject response = call.getResponseAsJson();
            Set statuses = new HashSet<>();
            StringBuilder errors = new StringBuilder();
            if (response != null){
                JSONObject data = (JSONObject)response.get("data");
                response = (JSONObject) response.get("response");

                JSONArray messages = (JSONArray) data.get("response_messages");
                if (messages == null) {
                    messages = new JSONArray();
                }
                messages.add(response);

                for (Object r : messages){
                    String status = (String)((JSONObject)r).get("status");
                    if (ObjectUtil.equals("error",status)){
                        String details = (String)((JSONObject)r).get("details");
                        if (!ObjectUtil.isVoid(details)){
                            errors.append(details);
                        }
                    }
                    statuses.add(status);
                }
            }
            if (call.hasErrors() || (statuses.contains("error"))){
                throw  new RuntimeException(errors.toString());
            }
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy