Skip to content

Commit

Permalink
Merge pull request #20 from Send4BR/dev
Browse files Browse the repository at this point in the history
Fix: ecommerce validation [main]
  • Loading branch information
Abner Persio authored Sep 28, 2022
2 parents 88c421a + 3d554d6 commit 9d7a193
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 32 deletions.
7 changes: 4 additions & 3 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ declare module '@aftersale/ecommerce-replication-sdk/lib/__mocks__/ecommerce-moc
integration: {
platform: string;
origin_domain: string;
store_id: null;
connectorUrl: string;
connectorFields: {
Url: string;
Expand Down Expand Up @@ -109,9 +110,9 @@ declare module '@aftersale/ecommerce-replication-sdk/lib/index' {

}
declare module '@aftersale/ecommerce-replication-sdk/lib/main' {
import { send } from "@aftersale/ecommerce-replication-sdk/lib/send";
import { Ecommerce } from "@aftersale/ecommerce-replication-sdk/lib/types/index";
import { ServiceBusSender } from "@azure/service-bus";
import { send } from '@aftersale/ecommerce-replication-sdk/lib/send';
import { Ecommerce } from '@aftersale/ecommerce-replication-sdk/lib/types/index';
import { ServiceBusSender } from '@azure/service-bus';
export function EcommerceReplicator(client: ServiceBusSender, sendEcommerce?: typeof send): {
send(ecommerce: Ecommerce): Promise<void>;
};
Expand Down
3 changes: 2 additions & 1 deletion dist/index.esm.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// lib/schema.ts
import S from "fluent-json-schema";
var ecommerceMessageSchema = S.object().prop("id", S.number()).prop("isCustomizable", S.boolean()).prop("title", S.string()).prop("customDetail", S.anyOf([S.string(), S.null()])).prop("description", S.anyOf([S.string(), S.null()])).prop("actionIds", S.array().items(S.number())).prop("tags", S.array().items(S.string()));
var schema = S.object().prop("uuid", S.string().required()).prop("enabled", S.boolean().required()).prop("name", S.string().required()).prop("storeUrl", S.string().required()).prop("trackingUrl", S.string().required()).prop("logoUrl", S.string().required()).prop("email", S.string().required()).prop("confirmationType", S.string().required()).prop("integrations", S.object().prop("platform", S.string().required()).prop("store_id", S.string().required()).prop("origin_domain", S.string().required()).prop("connectorUrl", S.string().required()).prop("connectorFields", S.object().required())).prop("channels", S.array().items(S.object().prop("id", S.string()).prop("primary", S.boolean()).prop("data", S.object()))).prop("features", S.array().items(S.string())).prop("featureSettings", S.object().prop("loyalty", S.object().prop("enabled", S.boolean().required()))).prop("utm", S.object().required()).prop("createdAt", S.string().required()).prop("updatedAt", S.string().required()).prop("coupons", S.object().prop("selected", S.anyOf([S.string(), S.null()])).prop("rules", S.anyOf([S.string(), S.null()])).prop("isActive", S.boolean().required()).prop("items", S.array().items(S.object().prop("name", S.anyOf([S.string(), S.null()])).prop("expirationDate", S.anyOf([S.string(), S.null()])).prop("discount", S.anyOf([S.number(), S.null()]))))).prop("notifications", S.array().items(S.number())).prop("demo", S.boolean()).prop("picking", S.number()).prop("handling", S.number()).prop("couriers", S.array().items(S.object().prop("customNames", S.array().items(S.string())).prop("sacMail", S.anyOf([S.string(), S.null()])).prop("enabled", S.boolean().required()).prop("id", S.string().required()).prop("courierUrl", S.anyOf([S.string(), S.null()])).prop("connectorUrl", S.anyOf([S.string(), S.null()])).prop("authFields", S.object()))).prop("mappedEcommerceStatuses", S.object().required()).prop("messsages", S.object().prop("courier", S.array().items(ecommerceMessageSchema).required()).prop("platform", S.array().items(ecommerceMessageSchema).required()).prop("system", S.array().items(ecommerceMessageSchema).required()));
var schema = S.object().prop("uuid", S.string().required()).prop("enabled", S.boolean().required()).prop("name", S.string().required()).prop("storeUrl", S.string().required()).prop("trackingUrl", S.string().required()).prop("logoUrl", S.string().required()).prop("email", S.string().required()).prop("confirmationType", S.string().required()).prop("integration", S.object().prop("platform", S.string().required()).prop("store_id", S.anyOf([S.string(), S.null()]).required()).prop("origin_domain", S.anyOf([S.string(), S.null()]).required()).prop("connectorUrl", S.string().required()).prop("connectorFields", S.object().required())).prop("channels", S.array().items(S.object().prop("id", S.string()).prop("primary", S.boolean()).prop("data", S.object()))).prop("features", S.array().items(S.string())).prop("featureSettings", S.object().prop("loyalty", S.object().prop("enabled", S.boolean().required()))).prop("utm", S.object().required()).prop("createdAt", S.string().required()).prop("updatedAt", S.string().required()).prop("coupons", S.object().prop("selected", S.anyOf([S.string(), S.null()])).prop("rules", S.anyOf([S.string(), S.null()])).prop("isActive", S.boolean().required()).prop("items", S.array().items(S.object().prop("name", S.anyOf([S.string(), S.null()])).prop("expirationDate", S.anyOf([S.string(), S.null()])).prop("discount", S.anyOf([S.number(), S.null()]))))).prop("notifications", S.array().items(S.number())).prop("demo", S.boolean()).prop("picking", S.number()).prop("handling", S.number()).prop("couriers", S.array().items(S.object().prop("customNames", S.array().items(S.string())).prop("sacMail", S.anyOf([S.string(), S.null()])).prop("enabled", S.boolean().required()).prop("id", S.string().required()).prop("courierUrl", S.anyOf([S.string(), S.null()])).prop("connectorUrl", S.anyOf([S.string(), S.null()])).prop("authFields", S.object()))).prop("mappedEcommerceStatuses", S.object().required()).prop("messsages", S.object().prop("courier", S.array().items(ecommerceMessageSchema).required()).prop("platform", S.array().items(ecommerceMessageSchema).required()).prop("system", S.array().items(ecommerceMessageSchema).required()));

// lib/send.ts
async function send({ client, ecommerce }) {
Expand All @@ -27,6 +27,7 @@ function EcommerceReplicator(client, sendEcommerce = send) {
const [isValid, errors] = EcommerceValidator().validate(ecommerce);
if (!isValid)
throw new Error(`Ecommerce validation fails, ${JSON.stringify(errors)}`);
console.info(`[Replication SDK] Replicating ecommerce ${ecommerce.uuid}`);
await sendEcommerce({ client, ecommerce });
}
};
Expand Down
3 changes: 2 additions & 1 deletion dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ __export(lib_exports, {
// lib/schema.ts
var import_fluent_json_schema = __toESM(require("fluent-json-schema"));
var ecommerceMessageSchema = import_fluent_json_schema.default.object().prop("id", import_fluent_json_schema.default.number()).prop("isCustomizable", import_fluent_json_schema.default.boolean()).prop("title", import_fluent_json_schema.default.string()).prop("customDetail", import_fluent_json_schema.default.anyOf([import_fluent_json_schema.default.string(), import_fluent_json_schema.default.null()])).prop("description", import_fluent_json_schema.default.anyOf([import_fluent_json_schema.default.string(), import_fluent_json_schema.default.null()])).prop("actionIds", import_fluent_json_schema.default.array().items(import_fluent_json_schema.default.number())).prop("tags", import_fluent_json_schema.default.array().items(import_fluent_json_schema.default.string()));
var schema = import_fluent_json_schema.default.object().prop("uuid", import_fluent_json_schema.default.string().required()).prop("enabled", import_fluent_json_schema.default.boolean().required()).prop("name", import_fluent_json_schema.default.string().required()).prop("storeUrl", import_fluent_json_schema.default.string().required()).prop("trackingUrl", import_fluent_json_schema.default.string().required()).prop("logoUrl", import_fluent_json_schema.default.string().required()).prop("email", import_fluent_json_schema.default.string().required()).prop("confirmationType", import_fluent_json_schema.default.string().required()).prop("integrations", import_fluent_json_schema.default.object().prop("platform", import_fluent_json_schema.default.string().required()).prop("store_id", import_fluent_json_schema.default.string().required()).prop("origin_domain", import_fluent_json_schema.default.string().required()).prop("connectorUrl", import_fluent_json_schema.default.string().required()).prop("connectorFields", import_fluent_json_schema.default.object().required())).prop("channels", import_fluent_json_schema.default.array().items(import_fluent_json_schema.default.object().prop("id", import_fluent_json_schema.default.string()).prop("primary", import_fluent_json_schema.default.boolean()).prop("data", import_fluent_json_schema.default.object()))).prop("features", import_fluent_json_schema.default.array().items(import_fluent_json_schema.default.string())).prop("featureSettings", import_fluent_json_schema.default.object().prop("loyalty", import_fluent_json_schema.default.object().prop("enabled", import_fluent_json_schema.default.boolean().required()))).prop("utm", import_fluent_json_schema.default.object().required()).prop("createdAt", import_fluent_json_schema.default.string().required()).prop("updatedAt", import_fluent_json_schema.default.string().required()).prop("coupons", import_fluent_json_schema.default.object().prop("selected", import_fluent_json_schema.default.anyOf([import_fluent_json_schema.default.string(), import_fluent_json_schema.default.null()])).prop("rules", import_fluent_json_schema.default.anyOf([import_fluent_json_schema.default.string(), import_fluent_json_schema.default.null()])).prop("isActive", import_fluent_json_schema.default.boolean().required()).prop("items", import_fluent_json_schema.default.array().items(import_fluent_json_schema.default.object().prop("name", import_fluent_json_schema.default.anyOf([import_fluent_json_schema.default.string(), import_fluent_json_schema.default.null()])).prop("expirationDate", import_fluent_json_schema.default.anyOf([import_fluent_json_schema.default.string(), import_fluent_json_schema.default.null()])).prop("discount", import_fluent_json_schema.default.anyOf([import_fluent_json_schema.default.number(), import_fluent_json_schema.default.null()]))))).prop("notifications", import_fluent_json_schema.default.array().items(import_fluent_json_schema.default.number())).prop("demo", import_fluent_json_schema.default.boolean()).prop("picking", import_fluent_json_schema.default.number()).prop("handling", import_fluent_json_schema.default.number()).prop("couriers", import_fluent_json_schema.default.array().items(import_fluent_json_schema.default.object().prop("customNames", import_fluent_json_schema.default.array().items(import_fluent_json_schema.default.string())).prop("sacMail", import_fluent_json_schema.default.anyOf([import_fluent_json_schema.default.string(), import_fluent_json_schema.default.null()])).prop("enabled", import_fluent_json_schema.default.boolean().required()).prop("id", import_fluent_json_schema.default.string().required()).prop("courierUrl", import_fluent_json_schema.default.anyOf([import_fluent_json_schema.default.string(), import_fluent_json_schema.default.null()])).prop("connectorUrl", import_fluent_json_schema.default.anyOf([import_fluent_json_schema.default.string(), import_fluent_json_schema.default.null()])).prop("authFields", import_fluent_json_schema.default.object()))).prop("mappedEcommerceStatuses", import_fluent_json_schema.default.object().required()).prop("messsages", import_fluent_json_schema.default.object().prop("courier", import_fluent_json_schema.default.array().items(ecommerceMessageSchema).required()).prop("platform", import_fluent_json_schema.default.array().items(ecommerceMessageSchema).required()).prop("system", import_fluent_json_schema.default.array().items(ecommerceMessageSchema).required()));
var schema = import_fluent_json_schema.default.object().prop("uuid", import_fluent_json_schema.default.string().required()).prop("enabled", import_fluent_json_schema.default.boolean().required()).prop("name", import_fluent_json_schema.default.string().required()).prop("storeUrl", import_fluent_json_schema.default.string().required()).prop("trackingUrl", import_fluent_json_schema.default.string().required()).prop("logoUrl", import_fluent_json_schema.default.string().required()).prop("email", import_fluent_json_schema.default.string().required()).prop("confirmationType", import_fluent_json_schema.default.string().required()).prop("integration", import_fluent_json_schema.default.object().prop("platform", import_fluent_json_schema.default.string().required()).prop("store_id", import_fluent_json_schema.default.anyOf([import_fluent_json_schema.default.string(), import_fluent_json_schema.default.null()]).required()).prop("origin_domain", import_fluent_json_schema.default.anyOf([import_fluent_json_schema.default.string(), import_fluent_json_schema.default.null()]).required()).prop("connectorUrl", import_fluent_json_schema.default.string().required()).prop("connectorFields", import_fluent_json_schema.default.object().required())).prop("channels", import_fluent_json_schema.default.array().items(import_fluent_json_schema.default.object().prop("id", import_fluent_json_schema.default.string()).prop("primary", import_fluent_json_schema.default.boolean()).prop("data", import_fluent_json_schema.default.object()))).prop("features", import_fluent_json_schema.default.array().items(import_fluent_json_schema.default.string())).prop("featureSettings", import_fluent_json_schema.default.object().prop("loyalty", import_fluent_json_schema.default.object().prop("enabled", import_fluent_json_schema.default.boolean().required()))).prop("utm", import_fluent_json_schema.default.object().required()).prop("createdAt", import_fluent_json_schema.default.string().required()).prop("updatedAt", import_fluent_json_schema.default.string().required()).prop("coupons", import_fluent_json_schema.default.object().prop("selected", import_fluent_json_schema.default.anyOf([import_fluent_json_schema.default.string(), import_fluent_json_schema.default.null()])).prop("rules", import_fluent_json_schema.default.anyOf([import_fluent_json_schema.default.string(), import_fluent_json_schema.default.null()])).prop("isActive", import_fluent_json_schema.default.boolean().required()).prop("items", import_fluent_json_schema.default.array().items(import_fluent_json_schema.default.object().prop("name", import_fluent_json_schema.default.anyOf([import_fluent_json_schema.default.string(), import_fluent_json_schema.default.null()])).prop("expirationDate", import_fluent_json_schema.default.anyOf([import_fluent_json_schema.default.string(), import_fluent_json_schema.default.null()])).prop("discount", import_fluent_json_schema.default.anyOf([import_fluent_json_schema.default.number(), import_fluent_json_schema.default.null()]))))).prop("notifications", import_fluent_json_schema.default.array().items(import_fluent_json_schema.default.number())).prop("demo", import_fluent_json_schema.default.boolean()).prop("picking", import_fluent_json_schema.default.number()).prop("handling", import_fluent_json_schema.default.number()).prop("couriers", import_fluent_json_schema.default.array().items(import_fluent_json_schema.default.object().prop("customNames", import_fluent_json_schema.default.array().items(import_fluent_json_schema.default.string())).prop("sacMail", import_fluent_json_schema.default.anyOf([import_fluent_json_schema.default.string(), import_fluent_json_schema.default.null()])).prop("enabled", import_fluent_json_schema.default.boolean().required()).prop("id", import_fluent_json_schema.default.string().required()).prop("courierUrl", import_fluent_json_schema.default.anyOf([import_fluent_json_schema.default.string(), import_fluent_json_schema.default.null()])).prop("connectorUrl", import_fluent_json_schema.default.anyOf([import_fluent_json_schema.default.string(), import_fluent_json_schema.default.null()])).prop("authFields", import_fluent_json_schema.default.object()))).prop("mappedEcommerceStatuses", import_fluent_json_schema.default.object().required()).prop("messsages", import_fluent_json_schema.default.object().prop("courier", import_fluent_json_schema.default.array().items(ecommerceMessageSchema).required()).prop("platform", import_fluent_json_schema.default.array().items(ecommerceMessageSchema).required()).prop("system", import_fluent_json_schema.default.array().items(ecommerceMessageSchema).required()));

// lib/send.ts
async function send({ client, ecommerce }) {
Expand All @@ -63,6 +63,7 @@ function EcommerceReplicator(client, sendEcommerce = send) {
const [isValid, errors] = EcommerceValidator().validate(ecommerce);
if (!isValid)
throw new Error(`Ecommerce validation fails, ${JSON.stringify(errors)}`);
console.info(`[Replication SDK] Replicating ecommerce ${ecommerce.uuid}`);
await sendEcommerce({ client, ecommerce });
}
};
Expand Down
2 changes: 1 addition & 1 deletion dist/tsconfig.tsbuildinfo

Large diffs are not rendered by default.

13 changes: 8 additions & 5 deletions lib/__mocks__/ecommerce-mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export default {
integration: {
platform: 'vtex',
origin_domain: 'mockecomm.test',
store_id: null,
connectorUrl: 'https://mockecomm.test',
connectorFields: {
Url: 'https://mockecomm.test',
Expand All @@ -28,11 +29,13 @@ export default {
selected: 'unique',
rules: 'CONFIA',
isActive: true,
items: [{
expirationDate: '2021-12-16T14:11:36.320Z',
name: '10OFF',
discount: null
}]
items: [
{
expirationDate: '2021-12-16T14:11:36.320Z',
name: '10OFF',
discount: null,
},
],
},
utm: {},
notifications: [12],
Expand Down
21 changes: 8 additions & 13 deletions lib/main.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { schema } from "./schema";
import { send } from "./send";
import { Ecommerce } from "./types";
import { ServiceBusSender } from "@azure/service-bus";
import Ajv from "ajv";
import { schema } from './schema';
import { send } from './send';
import { Ecommerce } from './types';
import { ServiceBusSender } from '@azure/service-bus';
import Ajv from 'ajv';

const ajv = new Ajv({ allErrors: true });
const validator = ajv.compile(schema.valueOf());
Expand All @@ -14,19 +14,14 @@ const EcommerceValidator = () => ({
},
});

export function EcommerceReplicator(
client: ServiceBusSender,
sendEcommerce = send
) {
export function EcommerceReplicator(client: ServiceBusSender, sendEcommerce = send) {
return {
async send(ecommerce: Ecommerce) {
const [isValid, errors] = EcommerceValidator().validate(ecommerce);

if (!isValid)
throw new Error(
`Ecommerce validation fails, ${JSON.stringify(errors)}`
);
if (!isValid) throw new Error(`Ecommerce validation fails, ${JSON.stringify(errors)}`);

console.info(`[Replication SDK] Replicating ecommerce ${ecommerce.uuid}`);
await sendEcommerce({ client, ecommerce });
},
};
Expand Down
20 changes: 12 additions & 8 deletions lib/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ export const schema = S.object()
.prop('email', S.string().required())
.prop('confirmationType', S.string().required())
.prop(
'integrations',
'integration',
S.object()
.prop('platform', S.string().required())
.prop('store_id', S.string().required())
.prop('origin_domain', S.string().required())
.prop('store_id', S.anyOf([S.string(), S.null()]).required())
.prop('origin_domain', S.anyOf([S.string(), S.null()]).required())
.prop('connectorUrl', S.string().required())
.prop('connectorFields', S.object().required())
)
Expand All @@ -47,11 +47,15 @@ export const schema = S.object()
.prop('selected', S.anyOf([S.string(), S.null()]))
.prop('rules', S.anyOf([S.string(), S.null()]))
.prop('isActive', S.boolean().required())
.prop('items', S.array().items(S.object()
.prop('name', S.anyOf([S.string(), S.null()]))
.prop('expirationDate', S.anyOf([S.string(), S.null()]))
.prop('discount', S.anyOf([S.number(), S.null()]))
))
.prop(
'items',
S.array().items(
S.object()
.prop('name', S.anyOf([S.string(), S.null()]))
.prop('expirationDate', S.anyOf([S.string(), S.null()]))
.prop('discount', S.anyOf([S.number(), S.null()]))
)
)
)
.prop('notifications', S.array().items(S.number()))
.prop('demo', S.boolean())
Expand Down

0 comments on commit 9d7a193

Please sign in to comment.