|
| 1 | +/* |
| 2 | + * Copyright 2023-present ByteChef Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.bytechef.component.bamboohr.trigger; |
| 18 | + |
| 19 | +import static com.bytechef.component.bamboohr.constant.BambooHrConstants.EMPLOYEE_NUMBER; |
| 20 | +import static com.bytechef.component.bamboohr.constant.BambooHrConstants.FIRST_NAME; |
| 21 | +import static com.bytechef.component.bamboohr.constant.BambooHrConstants.ID; |
| 22 | +import static com.bytechef.component.bamboohr.constant.BambooHrConstants.LAST_NAME; |
| 23 | +import static com.bytechef.component.definition.ComponentDsl.array; |
| 24 | +import static com.bytechef.component.definition.ComponentDsl.object; |
| 25 | +import static com.bytechef.component.definition.ComponentDsl.outputSchema; |
| 26 | +import static com.bytechef.component.definition.ComponentDsl.string; |
| 27 | +import static com.bytechef.component.definition.ComponentDsl.trigger; |
| 28 | + |
| 29 | +import com.bytechef.component.bamboohr.util.BambooHrUtils; |
| 30 | +import com.bytechef.component.definition.ComponentDsl; |
| 31 | +import com.bytechef.component.definition.Parameters; |
| 32 | +import com.bytechef.component.definition.TriggerContext; |
| 33 | +import com.bytechef.component.definition.TriggerDefinition; |
| 34 | +import com.bytechef.component.definition.TriggerDefinition.TriggerType; |
| 35 | +import com.bytechef.component.definition.TriggerDefinition.WebhookEnableOutput; |
| 36 | +import com.bytechef.component.definition.TypeReference; |
| 37 | +import java.util.ArrayList; |
| 38 | +import java.util.HashMap; |
| 39 | +import java.util.List; |
| 40 | +import java.util.Map; |
| 41 | + |
| 42 | +/** |
| 43 | + * @author Marija Horvat |
| 44 | + */ |
| 45 | +public class BambooHrUpdateEmployeeTrigger { |
| 46 | + |
| 47 | + public static final ComponentDsl.ModifiableTriggerDefinition TRIGGER_DEFINITION = trigger("updateEmployee") |
| 48 | + .title("Update Employee") |
| 49 | + .description("Triggers when specific employee fields are updated.") |
| 50 | + .type(TriggerType.DYNAMIC_WEBHOOK) |
| 51 | + .properties() |
| 52 | + .output(outputSchema( |
| 53 | + array() |
| 54 | + .items( |
| 55 | + object() |
| 56 | + .properties( |
| 57 | + string(FIRST_NAME), |
| 58 | + string(LAST_NAME), |
| 59 | + string(EMPLOYEE_NUMBER))))) |
| 60 | + .webhookEnable(BambooHrUpdateEmployeeTrigger::webhookEnable) |
| 61 | + .webhookDisable(BambooHrUpdateEmployeeTrigger::webhookDisable) |
| 62 | + .webhookRequest(BambooHrUpdateEmployeeTrigger::webhookRequest); |
| 63 | + |
| 64 | + private BambooHrUpdateEmployeeTrigger() { |
| 65 | + } |
| 66 | + |
| 67 | + protected static WebhookEnableOutput webhookEnable( |
| 68 | + Parameters inputParameters, Parameters connectionParameters, String webhookUrl, |
| 69 | + String workflowExecutionId, TriggerContext context) { |
| 70 | + |
| 71 | + return new WebhookEnableOutput( |
| 72 | + Map.of(ID, |
| 73 | + BambooHrUtils.addWebhook(webhookUrl, context)), |
| 74 | + null); |
| 75 | + } |
| 76 | + |
| 77 | + protected static void webhookDisable( |
| 78 | + Parameters inputParameters, Parameters connectionParameters, Parameters outputParameters, |
| 79 | + String workflowExecutionId, TriggerContext context) { |
| 80 | + |
| 81 | + BambooHrUtils.deleteWebhook(outputParameters, context); |
| 82 | + } |
| 83 | + |
| 84 | + protected static Object webhookRequest( |
| 85 | + Parameters inputParameters, Parameters connectionParameters, TriggerDefinition.HttpHeaders headers, |
| 86 | + TriggerDefinition.HttpParameters parameters, |
| 87 | + TriggerDefinition.WebhookBody body, TriggerDefinition.WebhookMethod method, |
| 88 | + TriggerDefinition.WebhookEnableOutput output, TriggerContext context) { |
| 89 | + |
| 90 | + List<Map<String, ?>> outputObject = new ArrayList<>(); |
| 91 | + |
| 92 | + if (body.getContent(new TypeReference<Map<String, ?>>() {}) |
| 93 | + .get("employees") instanceof List<?> list) { |
| 94 | + for (Object o : list) { |
| 95 | + if (o instanceof Map<?, ?> map) { |
| 96 | + Map<String, Map<String, ?>> fields = (Map<String, Map<String, ?>>) map.get("fields"); |
| 97 | + |
| 98 | + Map<String, Object> fieldMap = new HashMap<>(); |
| 99 | + |
| 100 | + for (Map.Entry<String, Map<String, ?>> fieldEntry : fields.entrySet()) { |
| 101 | + String fieldName = fieldEntry.getKey(); |
| 102 | + Map<String, ?> fieldValue = fieldEntry.getValue(); |
| 103 | + |
| 104 | + if (fieldValue.containsKey("value")) { |
| 105 | + fieldMap.put(fieldName, fieldValue.get("value")); |
| 106 | + } |
| 107 | + } |
| 108 | + outputObject.add(fieldMap); |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + return outputObject; |
| 113 | + } |
| 114 | +} |
0 commit comments