feat(core-flows,medusa,utils): promotion and campaign create/update endpoint (#6130)
what: - adds create endpoint for promotions including workflows and endpoint (RESOLVES CORE-1678) - adds update endpoint for promotions including workflows and endpoint (RESOLVES CORE-1679) - adds create endpoint for campaigns including workflows and endpoint (RESOLVES CORE-1684) - adds update endpoint for campaigns including workflows and endpoint (RESOLVES CORE-1685)
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
import { getSelectsAndRelationsFromObjectArray } from "../get-selects-and-relations-from-object-array"
|
||||
|
||||
describe("getSelectsAndRelationsFromObjectArray", function () {
|
||||
it("should return true or false for different types of data", function () {
|
||||
const expectations = [
|
||||
{
|
||||
input: [
|
||||
{
|
||||
attr_string: "string",
|
||||
attr_boolean: true,
|
||||
attr_null: null,
|
||||
attr_undefined: undefined,
|
||||
attr_object: {
|
||||
attr_string: "string",
|
||||
attr_boolean: true,
|
||||
attr_null: null,
|
||||
attr_undefined: undefined,
|
||||
},
|
||||
attr_array: [
|
||||
{
|
||||
attr_object: {
|
||||
attr_string: "string",
|
||||
attr_boolean: true,
|
||||
attr_null: null,
|
||||
attr_undefined: undefined,
|
||||
},
|
||||
},
|
||||
{
|
||||
attr_object: {
|
||||
attr_string: "string",
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
output: {
|
||||
selects: [
|
||||
"attr_string",
|
||||
"attr_boolean",
|
||||
"attr_null",
|
||||
"attr_undefined",
|
||||
"attr_object.attr_string",
|
||||
"attr_object.attr_boolean",
|
||||
"attr_object.attr_null",
|
||||
"attr_object.attr_undefined",
|
||||
"attr_array.attr_object.attr_string",
|
||||
"attr_array.attr_object.attr_boolean",
|
||||
"attr_array.attr_object.attr_null",
|
||||
"attr_array.attr_object.attr_undefined",
|
||||
],
|
||||
relations: ["attr_object", "attr_array", "attr_array.attr_object"],
|
||||
},
|
||||
},
|
||||
]
|
||||
|
||||
expectations.forEach((expectation) => {
|
||||
expect(getSelectsAndRelationsFromObjectArray(expectation.input)).toEqual(
|
||||
expectation.output
|
||||
)
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,53 @@
|
||||
import { deduplicate } from "./deduplicate"
|
||||
import { isObject } from "./is-object"
|
||||
|
||||
export function getSelectsAndRelationsFromObjectArray(
|
||||
dataArray: object[],
|
||||
prefix?: string
|
||||
): {
|
||||
selects: string[]
|
||||
relations: string[]
|
||||
} {
|
||||
const selects: string[] = []
|
||||
const relations: string[] = []
|
||||
|
||||
for (const data of dataArray) {
|
||||
for (const [key, value] of Object.entries(data)) {
|
||||
if (isObject(value)) {
|
||||
relations.push(setKey(key, prefix))
|
||||
const res = getSelectsAndRelationsFromObjectArray(
|
||||
[value],
|
||||
setKey(key, prefix)
|
||||
)
|
||||
selects.push(...res.selects)
|
||||
relations.push(...res.relations)
|
||||
} else if (Array.isArray(value)) {
|
||||
relations.push(setKey(key, prefix))
|
||||
const res = getSelectsAndRelationsFromObjectArray(
|
||||
value,
|
||||
setKey(key, prefix)
|
||||
)
|
||||
selects.push(...res.selects)
|
||||
relations.push(...res.relations)
|
||||
} else {
|
||||
selects.push(setKey(key, prefix))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const uniqueSelects: string[] = deduplicate(selects)
|
||||
const uniqueRelations: string[] = deduplicate(relations)
|
||||
|
||||
return {
|
||||
selects: uniqueSelects,
|
||||
relations: uniqueRelations,
|
||||
}
|
||||
}
|
||||
|
||||
function setKey(key: string, prefix?: string) {
|
||||
if (prefix) {
|
||||
return `${prefix}.${key}`
|
||||
} else {
|
||||
return key
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,7 @@ export * from "./errors"
|
||||
export * from "./generate-entity-id"
|
||||
export * from "./get-config-file"
|
||||
export * from "./get-iso-string-from-date"
|
||||
export * from "./get-selects-and-relations-from-object-array"
|
||||
export * from "./group-by"
|
||||
export * from "./handle-postgres-database-error"
|
||||
export * from "./is-date"
|
||||
|
||||
Reference in New Issue
Block a user