Skip to content

Commit

Permalink
add new functions to manage config schema
Browse files Browse the repository at this point in the history
  • Loading branch information
cparkertrivir committed Dec 17, 2024
1 parent 5860f44 commit 33e46b1
Showing 1 changed file with 146 additions and 0 deletions.
146 changes: 146 additions & 0 deletions src/ops/IdmConfigOps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,30 @@ export type IdmConfig = {
* @returns {IdObjectSkeletonInterface} promise resolving to a config entity
*/
deleteConfigEntity(entityId: string): Promise<IdObjectSkeletonInterface>;
/**
* Read an individual idm config schema.
* @param {string} entityId entity id for the schema that is being updated
* @param {string} name name of the specific schema that is being updated
* @param {ConfigEntityExportOptions} options import options
* @returns {Promise<IdObjectSkeletonInterface[]>} a promise resolving to an array of config entity objects
*/
readIndividualConfigEntitySchema(
entityId: string,
name: string,
options?: ConfigEntityExportOptions
): Promise<NoIdObjectSkeletonInterface>;
/**
* Import an individual idm config schema.
* @param {string} entityId entity id for the schema that is being updated
* @param {NoIdObjectSkeletonInterface} newConfigSchema the new idm config schema object
* @param {ConfigEntityImportOptions} options import options
* @returns {Promise<IdObjectSkeletonInterface[]>} a promise resolving to an array of config entity objects
*/
importIndividualConfigEntitySchema(
entityId: string,
newConfigSchema: IdObjectSkeletonInterface,
options?: ConfigEntityImportOptions
): Promise<IdObjectSkeletonInterface[]>;

// Deprecated

Expand Down Expand Up @@ -274,6 +298,33 @@ export default (state: State): IdmConfig => {
): Promise<IdObjectSkeletonInterface> {
return deleteConfigEntity({ entityId, state });
},
async readIndividualConfigEntitySchema(
entityId: string,
name: string,
options: ConfigEntityExportOptions = {
envReplaceParams: undefined,
entitiesToExport: undefined,
}
): Promise<NoIdObjectSkeletonInterface> {
return readIndividualConfigEntitySchema({
entityId,
name,
options,
state,
});
},
async importIndividualConfigEntitySchema(
entityId: string,
newConfigSchema: IdObjectSkeletonInterface,
options: ConfigEntityImportOptions = { validate: false }
): Promise<IdObjectSkeletonInterface[]> {
return importIndividualConfigEntitySchema({
entityId,
newConfigSchema,
options,
state,
});
},

// Deprecated

Expand Down Expand Up @@ -816,6 +867,101 @@ export async function deleteConfigEntity({
}
}

export async function readIndividualConfigEntitySchema({
entityId,
name,
options = { envReplaceParams: undefined, entitiesToExport: undefined },
state,
}: {
entityId: string;
name: string;
options?: ConfigEntityExportOptions;
state: State;
}): Promise<NoIdObjectSkeletonInterface> {
try {
const entity = substituteEntityWithEnv(
await readConfigEntity({ entityId, state }),
options.envReplaceParams
);

const schemaKey = Object.keys(entity).find((key) => key !== '_id');

if (!Array.isArray(entity[schemaKey])) {
throw new FrodoError(
`Error getting config entity schema ${entityId} ${name}`
);
}

const entitySchema = (
entity[schemaKey] as NoIdObjectSkeletonInterface[]
).find((item) => item.name === name);

if (!entitySchema) {
throw new FrodoError(
`Error getting config entity schema ${entityId} ${name}`
);
}
return entitySchema;
} catch (error) {
printError(error);
}
}

export async function importIndividualConfigEntitySchema({
entityId,
newConfigSchema,
options = {
envReplaceParams: undefined,
entitiesToImport: undefined,
validate: false,
},
state,
}: {
entityId: string;
newConfigSchema: IdObjectSkeletonInterface;
options: ConfigEntityImportOptions;
state: State;
}): Promise<IdObjectSkeletonInterface[]> {
try {
const entityExport = await exportConfigEntity({
entityId,
options,
state,
});

const schemaKey = Object.keys(entityExport.idm?.[entityId]).find(
(key) => key !== '_id'
);

if (!Array.isArray(entityExport.idm?.[entityId]?.[schemaKey])) {
throw new FrodoError(`Error updating config schema ${entityId}`);
}

const existingSchemaIndex = (
entityExport.idm?.[entityId]?.[schemaKey] as NoIdObjectSkeletonInterface[]
).findIndex((item) => item.name === newConfigSchema.name);

if (existingSchemaIndex !== -1) {
(entityExport.idm[entityId][schemaKey] as NoIdObjectSkeletonInterface[])[
existingSchemaIndex
] = newConfigSchema;
} else {
(
entityExport.idm[entityId][schemaKey] as NoIdObjectSkeletonInterface[]
).push(newConfigSchema);
}

return importConfigEntities({
entityId,
importData: entityExport,
options,
state,
});
} catch (error) {
throw new FrodoError(`Error updating config schema ${entityId}`, error);
}
}

function substituteEntityWithEnv(
entity: IdObjectSkeletonInterface,
envReplaceParams: string[][]
Expand Down

0 comments on commit 33e46b1

Please sign in to comment.