adding news functions of vendor in krowSDK

This commit is contained in:
José Salazar
2025-11-24 17:15:08 -05:00
parent 3afc0fdd23
commit d452c1c024

View File

@@ -155,7 +155,12 @@ const dataconnectEntityConfig = {
},
Vendor: {
list: 'listVendor',
get: 'getVendorById',
create: 'createVendor',
update: 'updateVendor',
delete: 'deleteVendor',
filter: 'filterVendors',
},
VendorRate: {
@@ -265,10 +270,137 @@ Object.entries(dataconnectEntityConfig).forEach(([entityName, ops]) => {
},
}),
// ...(ops.get && { get: async (params) => { ... } }),
// ...(ops.update && { update: async (params) => { ... } }),
// ...(ops.delete && { delete: async (params) => { ... } }),
// ...(ops.filter && { filter: async (params) => { ... } }),
//get
...(ops.get && {
get: async (params) => {
const fn = dcSdk[ops.get];
if (typeof fn !== 'function') {
throw new Error(
`Data Connect operation "${ops.get}" not found for entity "${entityName}".`
);
}
if (!params || typeof params !== 'object') {
throw new Error(`${entityName}.get expects an object of variables (e.g. { id })`);
}
return fn(dataConnect, params);
},
}),
//update
...(ops.update && {
update: async (params) => {
const fn = dcSdk[ops.update];
if (typeof fn !== 'function') {
throw new Error(
`Data Connect operation "${ops.update}" not found for entity "${entityName}".`
);
}
if (!params || typeof params !== 'object') {
throw new Error(
`${entityName}.update expects an object of variables matching the GraphQL mutation`
);
}
const { id, data } = params;
if (!id) {
throw new Error(`${entityName}.update requires an "id" field`);
}
if (!data || typeof data !== 'object') {
throw new Error(
`${entityName}.update requires a "data" object with the fields to update`
);
}
const vars = { id, ...data };
return fn(dataConnect, vars);
},
}),
// delete
...(ops.delete && {
delete: async (params) => {
const fn = dcSdk[ops.delete];
if (typeof fn !== 'function') {
throw new Error(
`Data Connect operation "${ops.delete}" not found for entity "${entityName}".`
);
}
if (!params || typeof params !== 'object') {
throw new Error(
`${entityName}.delete expects an object like { id }`
);
}
const { id } = params;
if (!id) {
throw new Error(`${entityName}.delete requires an "id" field`);
}
// Data Connect solo espera { id } como variables
return fn(dataConnect, { id });
},
}),
// filter
...(ops.filter && {
filter: async (params) => {
const fn = dcSdk[ops.filter];
if (typeof fn !== 'function') {
throw new Error(
`Data Connect operation "${ops.filter}" not found for entity "${entityName}".`
);
}
if (!params) {
if (ops.list) {//if no params, call to list()
const listFn = dcSdk[ops.list];
if (typeof listFn !== 'function') {
throw new Error(
`Data Connect operation "${ops.list}" not found for entity "${entityName}".`
);
}
return listFn(dataConnect);
}
throw new Error(`${entityName}.filter expects params or a list operation`);
}
const rawFilters = params.filters ?? params;
const variables = {};
for (const [key, value] of Object.entries(rawFilters)) {//cleaning undefined/null/'' values
if (value !== undefined && value !== null && value !== '') {
variables[key] = value;
}
}
// if no valid filters, call to list()
if (Object.keys(variables).length === 0) {
if (ops.list) {
const listFn = dcSdk[ops.list];
if (typeof listFn !== 'function') {
throw new Error(
`Data Connect operation "${ops.list}" not found for entity "${entityName}".`
);
}
return listFn(dataConnect);
}
throw new Error(`${entityName}.filter received no valid filters and no list operation`);
}
return fn(dataConnect, variables);
},
}),
};
});