import { CallExpressionShape, Plugin } from '@servicenow/sdk-build-core' import { NowIdShape } from './now-id-plugin' import { generateDeprecatedDiagnostics } from './utils' const crossScopeAliases = { targetName: ['target_name'], targetScope: ['target_scope'], targetType: ['target_type'], } /** * Creates a Cross Scope Privilege record (`sys_scope_privilege`). * * @see https://docs.servicenow.com/csh?topicname=c_CrossScopePrivilegeRecord.html&version=latest * * @param config - an object containing the following properties: * * **$id** - unique id for the record, typically using `Now.ID["value"]` * * **operation** - operation the script performs on the target * * **status** - authorization for this record * * **targetName** - name of the table, script include, or script object being requested * * **targetScope** - application whose resources are being requested * * **targetType** - type of request */ export const CrossScopePrivilegePlugin = Plugin.create({ name: 'CrossScopePrivilegePlugin', records: { sys_scope_privilege: { relationships: { sys_scope: { via: 'target_scope', inverse: true, }, }, toShape(record) { return { success: true, value: new CallExpressionShape({ source: record, callee: 'CrossScopePrivilege', args: [ record .transform(({ $ }) => ({ $id: $.val(NowIdShape.from(record)), operation: $, status: $, targetName: $.from('target_name'), targetScope: $.from('target_scope'), targetType: $.from('target_type'), })) .withAliasedKeys(crossScopeAliases), ], }), } }, }, }, shapes: [ { shape: CallExpressionShape, fileTypes: ['fluent'], async toRecord(callExpression, { factory, config, diagnostics }) { if (callExpression.getCallee() !== 'CrossScopePrivilege') { return { success: false } } const csp = callExpression.getArgument(0).asObject().withAliasedKeys(crossScopeAliases) generateDeprecatedDiagnostics(csp, diagnostics) const targetScope = csp.get('targetScope') const targetScopeRef = targetScope.isString() ? await factory.createReference({ source: targetScope, table: 'sys_scope', guid: targetScope, }) : undefined return { success: true, value: await factory.createRecord({ source: callExpression, table: 'sys_scope_privilege', explicitId: csp.get('$id'), properties: csp.transform(({ $ }) => ({ operation: $, status: $, target_name: $.from('targetName'), target_scope: $.val(targetScopeRef), target_type: $.from('targetType'), source_scope: $.val(config.scopeId), })), }), } }, }, ], })