Skip to content

Commit

Permalink
More flexible case body helper (#81)
Browse files Browse the repository at this point in the history
* Make case-body-helper much more flexible
* Add paf as valid namespace
  • Loading branch information
varney authored Aug 7, 2023
1 parent 7da9125 commit e91831a
Show file tree
Hide file tree
Showing 11 changed files with 421 additions and 223 deletions.
13 changes: 13 additions & 0 deletions .c8rc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"reporter": [
"text"
],
"exclude": [
"test",
"test-data"
],
"statements": "50",
"branches": "90",
"functions": "33",
"lines": "50"
}
13 changes: 0 additions & 13 deletions .nycrc

This file was deleted.

77 changes: 69 additions & 8 deletions lib/helpers/case-body-helper.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,80 @@
"use strict";

const ownerMapper = { dn: "DNBO" };
const { caseSchema } = require("../validation-helpers/schemas");

const sourceQueueMapper = { dn: "DNBO" };
const ownerMapper = {
"bbm-res": "BBM",
"bbm-dam": "BBM",
"bbm-aktuellhallbarhet": "BBM",
"bbm-fastighetsnytt": "BBM",
"bbm-byggindustrin": "BBM",
"bbm-dagensmedicin": "BBM",
"bbm-dagligvarunytt": "BBM",
"bbm-dagenssamhalle": "BBM",
"bbm-market": "BBM",
"bbm-news": "BBM",
bnlo: "MittMedia",
di: "Di_Backoffice_Omni",
dn: "DNBO",
expressen: "Expressen_Backoffice",
paf: "Di_Backoffice_Omni",
};

function createBackOfficeCaseBody(subject, description, namespace) {
return {
category: "Övriga ärenden",
const sourceQueueMapper = {
"bbm-res": "BBM_BackOffice",
"bbm-dam": "BBM_BackOffice",
"bbm-aktuellhallbarhet": "BBM_BackOffice",
"bbm-fastighetsnytt": "BBM_BackOffice",
"bbm-byggindustrin": "BBM_BackOffice",
"bbm-dagensmedicin": "BBM_BackOffice",
"bbm-dagligvarunytt": "BBM_BackOffice",
"bbm-dagenssamhalle": "BBM_BackOffice",
"bbm-market": "BBM_BackOffice",
"bbm-news": "BBM_BackOffice",
bnlo: "MittMedia_Secondline_Support",
di: "Di_Second_Line",
dn: "DNBO",
expressen: "Expressen_Backoffice",
paf: "PAF_Kundservice",
};

function createBackOfficeCaseBody(
subject,
description,
namespace,
{
businessType = "BN_B2C",
category = "Övriga ärenden",
contact,
deploymentName,
externalReference,
origin = "Greenfield",
owner,
priority = "Medium",
sourceQueue,
} = {}
) {
const caseBody = {
businessType,
category,
description,
namespace,
origin: "Greenfield",
owner: ownerMapper[namespace],
sourceQueue: sourceQueueMapper[namespace],
origin,
owner: owner || ownerMapper[namespace],
priority,
sourceQueue: sourceQueue || sourceQueueMapper[namespace],
subject,
};

if (contact) caseBody.contact = contact;
if (deploymentName) caseBody.deploymentName = deploymentName;
if (externalReference) caseBody.externalReference = externalReference;

const result = caseSchema.validate(caseBody);

if (result.error) throw result.error;

return caseBody;
}

module.exports = createBackOfficeCaseBody;
4 changes: 4 additions & 0 deletions lib/namespaces.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
const postalDeliveryOnlyNamespaces = [ "paf" ];

const commonNamespaces = [ "dn", "expressen", "bnlo" ];
const platformNamespaces = [ ...commonNamespaces, "paf" ];
const salesforceCaseNamespaces = [ ...platformNamespaces, "di" ];

function postalDeliveryOnly(namespace) {
return postalDeliveryOnlyNamespaces.includes(namespace);
Expand All @@ -20,4 +22,6 @@ module.exports = {
getCommonNamespaces,
isCommonNamespace,
postalDeliveryOnly,
platformNamespaces,
salesforceCaseNamespaces,
};
27 changes: 27 additions & 0 deletions lib/validation-helpers/schemas.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ const Basejoi = require("joi");
const joi = Basejoi.extend(require("joi-postalcode"));
const validCountries = require("../validation-helpers/country-codes");
const { dateRegex } = require("./formatting-helpers");
const { salesforceCaseNamespaces } = require("../namespaces");

const requiredString = joi.string().min(1).required();

const customZipCodeValidation = function (value) {
// Return empty object if address is empty.
Expand Down Expand Up @@ -75,8 +78,32 @@ const distributionFeeSchema = joi.object().keys({
monthlyDelay: joi.number().precision(0).allow(0).tag("white_list"),
});

// ---------------------- Salesforce schemas ----------------------

const caseSchema = joi.object().keys({
namespace: requiredString.valid(...salesforceCaseNamespaces),
businessType: joi.string().valid("BN_B2C", "BN_B2B"),
category: joi.string(),
contact: joi.object().keys({
firstName: joi.string(),
lastName: joi.string(),
email: requiredString,
phone: joi.string().regex(/^[+\d][0-9]{7,14}$/),
customerNumber: joi.string(),
}).optional(),
deploymentName: joi.string(),
description: requiredString,
externalReference: joi.string().optional(),
origin: requiredString,
owner: requiredString,
priority: joi.string().optional().valid("High", "Medium", "Low"),
sourceQueue: requiredString,
subject: requiredString,
});

module.exports = {
addressSchema,
caseSchema,
distributionFeeSchemaNand: distributionFeeSchema.nand("startDate", "monthlyDelay"),
distributionFeeSchemaXor: distributionFeeSchema.xor("startDate", "monthlyDelay"),
};
5 changes: 2 additions & 3 deletions lib/validation-helpers/strip-joi-schema-tags.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function flattenKeys(o) {
const output = {};
while (stack.length) {
const { keys: current, previousKey } = stack.shift();
const keys = current instanceof Object ? Object.keys(current) : [];
const keys = current instanceof Object ? Object.keys(current) : []; // FIXME: not needed, current will always be an Object because of joi

for (const key of keys) {
const value = current[key];
Expand All @@ -48,8 +48,7 @@ function getFlaggedKeys(o) {
const output = {};
while (stack.length) {
const { keys: current, previousKey } = stack.shift();

const keys = current instanceof Object ? Object.keys(current) : [];
const keys = current instanceof Object ? Object.keys(current) : []; // FIXME: not needed, current will always be an Object because of joi

for (const key of keys) {
const value = current[key];
Expand Down
Loading

0 comments on commit e91831a

Please sign in to comment.