diff --git a/apps-script/3p-resources/3p-resources.gs b/apps-script/3p-resources/3p-resources.gs index 9b25f25..ea2d159 100644 --- a/apps-script/3p-resources/3p-resources.gs +++ b/apps-script/3p-resources/3p-resources.gs @@ -52,7 +52,7 @@ function caseLinkPreview(event) { * @return {!Map} A map with the extracted URL parameters. */ function parseQuery(url) { - var query = url.split("?")[1]; + const query = url.split("?")[1]; if (query) { return query.split("&") .reduce(function(o, e) { @@ -87,20 +87,20 @@ function parseQuery(url) { */ function createCaseInputCard(event, errors, isUpdate) { - const cardHeader1 = CardService.newCardHeader() + const cardHeader = CardService.newCardHeader() .setTitle('Create a support case') - const cardSection1TextInput1 = CardService.newTextInput() + const cardSectionTextInput1 = CardService.newTextInput() .setFieldName('name') .setTitle('Name') .setMultiline(false); - const cardSection1TextInput2 = CardService.newTextInput() + const cardSectionTextInput2 = CardService.newTextInput() .setFieldName('description') .setTitle('Description') .setMultiline(true); - const cardSection1SelectionInput1 = CardService.newSelectionInput() + const cardSectionSelectionInput1 = CardService.newSelectionInput() .setFieldName('priority') .setTitle('Priority') .setType(CardService.SelectionInputType.DROPDOWN) @@ -109,49 +109,49 @@ function createCaseInputCard(event, errors, isUpdate) { .addItem('P2', 'P2', false) .addItem('P3', 'P3', false); - const cardSection1SelectionInput2 = CardService.newSelectionInput() + const cardSectionSelectionInput2 = CardService.newSelectionInput() .setFieldName('impact') .setTitle('Impact') .setType(CardService.SelectionInputType.CHECK_BOX) .addItem('Blocks a critical customer operation', 'Blocks a critical customer operation', false); - const cardSection1ButtonList1Button1Action1 = CardService.newAction() + const cardSectionButtonListButtonAction = CardService.newAction() .setPersistValues(true) .setFunctionName('submitCaseCreationForm') .setParameters({}); - const cardSection1ButtonList1Button1 = CardService.newTextButton() + const cardSectionButtonListButton = CardService.newTextButton() .setText('Create') .setTextButtonStyle(CardService.TextButtonStyle.TEXT) - .setOnClickAction(cardSection1ButtonList1Button1Action1); + .setOnClickAction(cardSectionButtonListButtonAction); - const cardSection1ButtonList1 = CardService.newButtonSet() - .addButton(cardSection1ButtonList1Button1); + const cardSectionButtonList = CardService.newButtonSet() + .addButton(cardSectionButtonListButton); // Builds the form inputs with error texts for invalid values. - const cardSection1 = CardService.newCardSection(); + const cardSection = CardService.newCardSection(); if (errors?.name) { - cardSection1.addWidget(createErrorTextParagraph(errors.name)); + cardSection.addWidget(createErrorTextParagraph(errors.name)); } - cardSection1.addWidget(cardSection1TextInput1); + cardSection.addWidget(cardSectionTextInput1); if (errors?.description) { - cardSection1.addWidget(createErrorTextParagraph(errors.description)); + cardSection.addWidget(createErrorTextParagraph(errors.description)); } - cardSection1.addWidget(cardSection1TextInput2); + cardSection.addWidget(cardSectionTextInput2); if (errors?.priority) { - cardSection1.addWidget(createErrorTextParagraph(errors.priority)); + cardSection.addWidget(createErrorTextParagraph(errors.priority)); } - cardSection1.addWidget(cardSection1SelectionInput1); + cardSection.addWidget(cardSectionSelectionInput1); if (errors?.impact) { - cardSection1.addWidget(createErrorTextParagraph(errors.impact)); + cardSection.addWidget(createErrorTextParagraph(errors.impact)); } - cardSection1.addWidget(cardSection1SelectionInput2); - cardSection1.addWidget(cardSection1ButtonList1); + cardSection.addWidget(cardSectionSelectionInput2); + cardSection.addWidget(cardSectionButtonList); const card = CardService.newCardBuilder() - .setHeader(cardHeader1) - .addSection(cardSection1) + .setHeader(cardHeader) + .addSection(cardSection) .build(); if (isUpdate) { @@ -188,11 +188,23 @@ function submitCaseCreationForm(event) { } else { const title = `Case ${caseDetails.name}`; // Adds the case details as parameters to the generated link URL. - const url = 'https://example.com/support/cases/?' + Object.entries(caseDetails).flatMap(([k, v]) => Array.isArray(v) ? v.map(e => `${k}=${encodeURIComponent(e)}`) : `${k}=${encodeURIComponent(v)}`).join("&"); + const url = 'https://example.com/support/cases/?' + generateQuery(caseDetails); return createLinkRenderAction(title, url); } } +/** +* Build a query path with URL parameters. +* +* @param {!Map} parameters A map with the URL parameters. +* @return {!string} The resulting query path. +*/ +function generateQuery(parameters) { + return Object.entries(parameters).flatMap(([k, v]) => + Array.isArray(v) ? v.map(e => `${k}=${encodeURIComponent(e)}`) : `${k}=${encodeURIComponent(v)}` + ).join("&"); +} + // [END add_ons_3p_resources_submit_create_case] // [START add_ons_3p_resources_validate_inputs] @@ -205,16 +217,16 @@ function submitCaseCreationForm(event) { */ function validateFormInputs(caseDetails) { const errors = {}; - if (caseDetails.name === undefined) { + if (!caseDetails.name) { errors.name = 'You must provide a name'; } - if (caseDetails.description === undefined) { + if (!caseDetails.description) { errors.description = 'You must provide a description'; } - if (caseDetails.priority === undefined) { + if (!caseDetails.priority) { errors.priority = 'You must provide a priority'; } - if (caseDetails.impact && !(['P0', 'P1']).includes(caseDetails.priority)) { + if (caseDetails.impact && caseDetails.priority !== 'P0' && caseDetails.priority !== 'P1') { errors.impact = 'If an issue blocks a critical customer operation, priority must be P0 or P1'; } diff --git a/java/3p-resources/src/main/java/Create3pResources.java b/java/3p-resources/src/main/java/Create3pResources.java index a17c7a9..2fc05b2 100644 --- a/java/3p-resources/src/main/java/Create3pResources.java +++ b/java/3p-resources/src/main/java/Create3pResources.java @@ -44,10 +44,8 @@ public void service(HttpRequest request, HttpResponse response) throws Exception JsonObject parameters = event.getAsJsonObject("commonEventObject").getAsJsonObject("parameters"); if (parameters != null && parameters.has("submitCaseCreationForm") && parameters.get("submitCaseCreationForm").getAsBoolean()) { response.getWriter().write(gson.toJson(submitCaseCreationForm(event))); - return; } else { response.getWriter().write(gson.toJson(createCaseInputCard(event, new HashMap(), false))); - return; } } @@ -62,128 +60,127 @@ public void service(HttpRequest request, HttpResponse response) throws Exception * @return The resulting card or action response. */ JsonObject createCaseInputCard(JsonObject event, Map errors, boolean isUpdate) { - - JsonObject cardHeader1 = new JsonObject(); - cardHeader1.add("title", new JsonPrimitive("Create a support case")); - - JsonObject cardSection1TextInput1 = new JsonObject(); - cardSection1TextInput1.add("name", new JsonPrimitive("name")); - cardSection1TextInput1.add("label", new JsonPrimitive("Name")); - - JsonObject cardSection1TextInput1Widget = new JsonObject(); - cardSection1TextInput1Widget.add("textInput", cardSection1TextInput1); - - JsonObject cardSection1TextInput2 = new JsonObject(); - cardSection1TextInput2.add("name", new JsonPrimitive("description")); - cardSection1TextInput2.add("label", new JsonPrimitive("Description")); - cardSection1TextInput2.add("type", new JsonPrimitive("MULTIPLE_LINE")); - - JsonObject cardSection1TextInput2Widget = new JsonObject(); - cardSection1TextInput2Widget.add("textInput", cardSection1TextInput2); - - JsonObject cardSection1SelectionInput1ItemsItem1 = new JsonObject(); - cardSection1SelectionInput1ItemsItem1.add("text", new JsonPrimitive("P0")); - cardSection1SelectionInput1ItemsItem1.add("value", new JsonPrimitive("P0")); - - JsonObject cardSection1SelectionInput1ItemsItem2 = new JsonObject(); - cardSection1SelectionInput1ItemsItem2.add("text", new JsonPrimitive("P1")); - cardSection1SelectionInput1ItemsItem2.add("value", new JsonPrimitive("P1")); - - JsonObject cardSection1SelectionInput1ItemsItem3 = new JsonObject(); - cardSection1SelectionInput1ItemsItem3.add("text", new JsonPrimitive("P2")); - cardSection1SelectionInput1ItemsItem3.add("value", new JsonPrimitive("P2")); - - JsonObject cardSection1SelectionInput1ItemsItem4 = new JsonObject(); - cardSection1SelectionInput1ItemsItem4.add("text", new JsonPrimitive("P3")); - cardSection1SelectionInput1ItemsItem4.add("value", new JsonPrimitive("P3")); - - JsonArray cardSection1SelectionInput1Items = new JsonArray(); - cardSection1SelectionInput1Items.add(cardSection1SelectionInput1ItemsItem1); - cardSection1SelectionInput1Items.add(cardSection1SelectionInput1ItemsItem2); - cardSection1SelectionInput1Items.add(cardSection1SelectionInput1ItemsItem3); - cardSection1SelectionInput1Items.add(cardSection1SelectionInput1ItemsItem4); - - JsonObject cardSection1SelectionInput1 = new JsonObject(); - cardSection1SelectionInput1.add("name", new JsonPrimitive("priority")); - cardSection1SelectionInput1.add("label", new JsonPrimitive("Priority")); - cardSection1SelectionInput1.add("type", new JsonPrimitive("DROPDOWN")); - cardSection1SelectionInput1.add("items", cardSection1SelectionInput1Items); - - JsonObject cardSection1SelectionInput1Widget = new JsonObject(); - cardSection1SelectionInput1Widget.add("selectionInput", cardSection1SelectionInput1); - - JsonObject cardSection1SelectionInput2ItemsItem1 = new JsonObject(); - cardSection1SelectionInput2ItemsItem1.add("text", new JsonPrimitive("Blocks a critical customer operation")); - cardSection1SelectionInput2ItemsItem1.add("value", new JsonPrimitive("Blocks a critical customer operation")); + JsonObject cardHeader = new JsonObject(); + cardHeader.add("title", new JsonPrimitive("Create a support case")); + + JsonObject cardSectionTextInput1 = new JsonObject(); + cardSectionTextInput1.add("name", new JsonPrimitive("name")); + cardSectionTextInput1.add("label", new JsonPrimitive("Name")); + + JsonObject cardSectionTextInput1Widget = new JsonObject(); + cardSectionTextInput1Widget.add("textInput", cardSectionTextInput1); + + JsonObject cardSectionTextInput2 = new JsonObject(); + cardSectionTextInput2.add("name", new JsonPrimitive("description")); + cardSectionTextInput2.add("label", new JsonPrimitive("Description")); + cardSectionTextInput2.add("type", new JsonPrimitive("MULTIPLE_LINE")); + + JsonObject cardSectionTextInput2Widget = new JsonObject(); + cardSectionTextInput2Widget.add("textInput", cardSectionTextInput2); + + JsonObject cardSectionSelectionInput1ItemsItem1 = new JsonObject(); + cardSectionSelectionInput1ItemsItem1.add("text", new JsonPrimitive("P0")); + cardSectionSelectionInput1ItemsItem1.add("value", new JsonPrimitive("P0")); + + JsonObject cardSectionSelectionInput1ItemsItem2 = new JsonObject(); + cardSectionSelectionInput1ItemsItem2.add("text", new JsonPrimitive("P1")); + cardSectionSelectionInput1ItemsItem2.add("value", new JsonPrimitive("P1")); + + JsonObject cardSectionSelectionInput1ItemsItem3 = new JsonObject(); + cardSectionSelectionInput1ItemsItem3.add("text", new JsonPrimitive("P2")); + cardSectionSelectionInput1ItemsItem3.add("value", new JsonPrimitive("P2")); + + JsonObject cardSectionSelectionInput1ItemsItem4 = new JsonObject(); + cardSectionSelectionInput1ItemsItem4.add("text", new JsonPrimitive("P3")); + cardSectionSelectionInput1ItemsItem4.add("value", new JsonPrimitive("P3")); + + JsonArray cardSectionSelectionInput1Items = new JsonArray(); + cardSectionSelectionInput1Items.add(cardSectionSelectionInput1ItemsItem1); + cardSectionSelectionInput1Items.add(cardSectionSelectionInput1ItemsItem2); + cardSectionSelectionInput1Items.add(cardSectionSelectionInput1ItemsItem3); + cardSectionSelectionInput1Items.add(cardSectionSelectionInput1ItemsItem4); + + JsonObject cardSectionSelectionInput1 = new JsonObject(); + cardSectionSelectionInput1.add("name", new JsonPrimitive("priority")); + cardSectionSelectionInput1.add("label", new JsonPrimitive("Priority")); + cardSectionSelectionInput1.add("type", new JsonPrimitive("DROPDOWN")); + cardSectionSelectionInput1.add("items", cardSectionSelectionInput1Items); + + JsonObject cardSectionSelectionInput1Widget = new JsonObject(); + cardSectionSelectionInput1Widget.add("selectionInput", cardSectionSelectionInput1); + + JsonObject cardSectionSelectionInput2ItemsItem = new JsonObject(); + cardSectionSelectionInput2ItemsItem.add("text", new JsonPrimitive("Blocks a critical customer operation")); + cardSectionSelectionInput2ItemsItem.add("value", new JsonPrimitive("Blocks a critical customer operation")); - JsonArray cardSection1SelectionInput2Items = new JsonArray(); - cardSection1SelectionInput2Items.add(cardSection1SelectionInput2ItemsItem1); + JsonArray cardSectionSelectionInput2Items = new JsonArray(); + cardSectionSelectionInput2Items.add(cardSectionSelectionInput2ItemsItem); - JsonObject cardSection1SelectionInput2 = new JsonObject(); - cardSection1SelectionInput2.add("name", new JsonPrimitive("impact")); - cardSection1SelectionInput2.add("label", new JsonPrimitive("Impact")); - cardSection1SelectionInput2.add("items", cardSection1SelectionInput2Items); + JsonObject cardSectionSelectionInput2 = new JsonObject(); + cardSectionSelectionInput2.add("name", new JsonPrimitive("impact")); + cardSectionSelectionInput2.add("label", new JsonPrimitive("Impact")); + cardSectionSelectionInput2.add("items", cardSectionSelectionInput2Items); - JsonObject cardSection1SelectionInput2Widget = new JsonObject(); - cardSection1SelectionInput2Widget.add("selectionInput", cardSection1SelectionInput2); + JsonObject cardSectionSelectionInput2Widget = new JsonObject(); + cardSectionSelectionInput2Widget.add("selectionInput", cardSectionSelectionInput2); - JsonObject cardSection1ButtonList1Button1Action1ParametersParameter1 = new JsonObject(); - cardSection1ButtonList1Button1Action1ParametersParameter1.add("key", new JsonPrimitive("submitCaseCreationForm")); - cardSection1ButtonList1Button1Action1ParametersParameter1.add("value", new JsonPrimitive(true)); + JsonObject cardSectionButtonListButtonActionParametersParameter = new JsonObject(); + cardSectionButtonListButtonActionParametersParameter.add("key", new JsonPrimitive("submitCaseCreationForm")); + cardSectionButtonListButtonActionParametersParameter.add("value", new JsonPrimitive(true)); - JsonArray cardSection1ButtonList1Button1Action1Parameters = new JsonArray(); - cardSection1ButtonList1Button1Action1Parameters.add(cardSection1ButtonList1Button1Action1ParametersParameter1); + JsonArray cardSectionButtonListButtonActionParameters = new JsonArray(); + cardSectionButtonListButtonActionParameters.add(cardSectionButtonListButtonActionParametersParameter); - JsonObject cardSection1ButtonList1Button1Action1 = new JsonObject(); - cardSection1ButtonList1Button1Action1.add("function", new JsonPrimitive(System.getenv().get("URL"))); - cardSection1ButtonList1Button1Action1.add("parameters", cardSection1ButtonList1Button1Action1Parameters); - cardSection1ButtonList1Button1Action1.add("persistValues", new JsonPrimitive(true)); + JsonObject cardSectionButtonListButtonAction = new JsonObject(); + cardSectionButtonListButtonAction.add("function", new JsonPrimitive(System.getenv().get("URL"))); + cardSectionButtonListButtonAction.add("parameters", cardSectionButtonListButtonActionParameters); + cardSectionButtonListButtonAction.add("persistValues", new JsonPrimitive(true)); - JsonObject cardSection1ButtonList1Button1OnCLick = new JsonObject(); - cardSection1ButtonList1Button1OnCLick.add("action", cardSection1ButtonList1Button1Action1); + JsonObject cardSectionButtonListButtonOnCLick = new JsonObject(); + cardSectionButtonListButtonOnCLick.add("action", cardSectionButtonListButtonAction); - JsonObject cardSection1ButtonList1Button1 = new JsonObject(); - cardSection1ButtonList1Button1.add("text", new JsonPrimitive("Create")); - cardSection1ButtonList1Button1.add("onClick", cardSection1ButtonList1Button1OnCLick); + JsonObject cardSectionButtonListButton = new JsonObject(); + cardSectionButtonListButton.add("text", new JsonPrimitive("Create")); + cardSectionButtonListButton.add("onClick", cardSectionButtonListButtonOnCLick); - JsonArray cardSection1ButtonList1Buttons = new JsonArray(); - cardSection1ButtonList1Buttons.add(cardSection1ButtonList1Button1); + JsonArray cardSectionButtonListButtons = new JsonArray(); + cardSectionButtonListButtons.add(cardSectionButtonListButton); - JsonObject cardSection1ButtonList1 = new JsonObject(); - cardSection1ButtonList1.add("buttons", cardSection1ButtonList1Buttons); + JsonObject cardSectionButtonList = new JsonObject(); + cardSectionButtonList.add("buttons", cardSectionButtonListButtons); - JsonObject cardSection1ButtonList1Widget = new JsonObject(); - cardSection1ButtonList1Widget.add("buttonList", cardSection1ButtonList1); + JsonObject cardSectionButtonListWidget = new JsonObject(); + cardSectionButtonListWidget.add("buttonList", cardSectionButtonList); // Builds the form inputs with error texts for invalid values. - JsonArray cardSection1 = new JsonArray(); + JsonArray cardSection = new JsonArray(); if (errors.containsKey("name")) { - cardSection1.add(createErrorTextParagraph(errors.get("name").toString())); + cardSection.add(createErrorTextParagraph(errors.get("name").toString())); } - cardSection1.add(cardSection1TextInput1Widget); + cardSection.add(cardSectionTextInput1Widget); if (errors.containsKey("description")) { - cardSection1.add(createErrorTextParagraph(errors.get("description").toString())); + cardSection.add(createErrorTextParagraph(errors.get("description").toString())); } - cardSection1.add(cardSection1TextInput2Widget); + cardSection.add(cardSectionTextInput2Widget); if (errors.containsKey("priority")) { - cardSection1.add(createErrorTextParagraph(errors.get("priority").toString())); + cardSection.add(createErrorTextParagraph(errors.get("priority").toString())); } - cardSection1.add(cardSection1SelectionInput1Widget); + cardSection.add(cardSectionSelectionInput1Widget); if (errors.containsKey("impact")) { - cardSection1.add(createErrorTextParagraph(errors.get("impact").toString())); + cardSection.add(createErrorTextParagraph(errors.get("impact").toString())); } - cardSection1.add(cardSection1SelectionInput2Widget); - cardSection1.add(cardSection1ButtonList1Widget); + cardSection.add(cardSectionSelectionInput2Widget); + cardSection.add(cardSectionButtonListWidget); - JsonObject cardSection1Widgets = new JsonObject(); - cardSection1Widgets.add("widgets", cardSection1); + JsonObject cardSectionWidgets = new JsonObject(); + cardSectionWidgets.add("widgets", cardSection); JsonArray sections = new JsonArray(); - sections.add(cardSection1Widgets); + sections.add(cardSectionWidgets); JsonObject card = new JsonObject(); - card.add("header", cardHeader1); + card.add("header", cardHeader); card.add("sections", sections); JsonObject navigation = new JsonObject(); @@ -310,12 +307,12 @@ JsonObject createErrorTextParagraph(String errorMessage) { * @return The resulting submit form response. */ JsonObject createLinkRenderAction(String title, String url) { - JsonObject link1 = new JsonObject(); - link1.add("title", new JsonPrimitive(title)); - link1.add("url", new JsonPrimitive(url)); + JsonObject link = new JsonObject(); + link.add("title", new JsonPrimitive(title)); + link.add("url", new JsonPrimitive(url)); JsonArray links = new JsonArray(); - links.add(link1); + links.add(link); JsonObject action = new JsonObject(); action.add("links", links);