PSA/ee/server/seeds/onboarding/algadesk/02_permissions.cjs
Hermes 284313f908
Some checks are pending
Bidi Control Character Guard / bidi-control-guard (push) Waiting to run
Circular Dependency Check / Check for new circular dependencies (push) Waiting to run
Citus Migration Smoke / Combined migrations on single-node Citus (push) Waiting to run
E2E Fresh Install Tests / fresh-install-e2e (push) Waiting to run
ext-v2 guardrails / Run ext-v2 guard and ESLint (push) Waiting to run
Integration Tests / Check for relevant changes (push) Waiting to run
Integration Tests / ${{ (github.event_name == 'schedule' || github.event.inputs.suite == 'full') && 'Full integration suite' || 'Tier-1 integration subset' }} (push) Blocked by required conditions
Mobile checks / Mobile lint + typecheck (push) Waiting to run
Mobile checks / Mobile unit tests (push) Waiting to run
Mobile checks / Mobile dependency audit (report) (push) Waiting to run
Mobile checks / Mobile reproducibility checks (push) Waiting to run
Secrets guard (env backups) / Ensure no tracked env backup files (push) Waiting to run
Temporal Readiness / fast-readiness (push) Waiting to run
Temporal Readiness / docker-parity (push) Waiting to run
TypeScript Type Check / Nx affected typecheck (push) Waiting to run
Unit Tests / Skipped-test budget (push) Waiting to run
Unit Tests / Nx affected unit tests (push) Waiting to run
Unit Tests / Server unit coverage (informational) (push) Waiting to run
Validate Tenant Management Schema / Check for relevant changes (push) Waiting to run
Validate Tenant Management Schema / Validate Tenant Management Schema (push) Blocked by required conditions
EE Workflows Build Guard / ee-workflows-build-guard (push) Waiting to run
Initial import of AlgaPSA codebase from PSA server
Excluded: .git, node_modules, secrets/, compose.env, assemblyscript tgz

Source: /opt/alga-psa on psa.joliet.tech
2026-06-22 16:12:17 -05:00

83 lines
3.5 KiB
JavaScript

exports.seed = async function(knex, tenantId) {
let tenants;
if (tenantId) {
tenants = [{ tenant: tenantId }];
} else {
tenants = await knex('tenants').where({ product_code: 'algadesk' }).select('tenant');
if (!tenants.length) {
console.log('No Algadesk tenants found, skipping permissions seed');
return;
}
}
const mspPermissions = [
['client', ['create', 'read', 'update', 'delete'], 'clients'],
['contact', ['create', 'read', 'update', 'delete'], 'contacts'],
['document', ['create', 'read', 'update', 'delete'], 'documents'],
['profile', ['read', 'update'], 'profiles'],
['reports', ['create', 'read', 'update', 'delete'], 'reports'],
['settings', ['read', 'update'], 'settings'],
['system_settings', ['read', 'update'], 'system settings'],
['security_settings', ['read', 'update'], 'security settings'],
['tag', ['create', 'read', 'update', 'delete'], 'tags'],
['ticket', ['create', 'read', 'update', 'delete'], 'tickets'],
['ticket_settings', ['create', 'read', 'update', 'delete'], 'ticket settings'],
['user', ['create', 'read', 'update', 'delete', 'invite', 'reset_password'], 'users'],
['user_settings', ['create', 'read', 'update', 'delete'], 'user and team settings']
];
const clientPermissions = [
['client', ['read', 'update'], 'client information'],
['contact', ['read', 'update'], 'contacts'],
['document', ['create', 'read', 'update'], 'documents'],
['settings', ['read', 'update'], 'portal settings'],
['ticket', ['create', 'read', 'update', 'delete'], 'tickets'],
['user', ['create', 'read', 'update', 'delete', 'reset_password'], 'client portal users']
];
const allPermissions = [
...mspPermissions.flatMap(([resource, actions, label]) =>
actions.map(action => ({
resource,
action,
msp: true,
client: false,
description: `${action.charAt(0).toUpperCase()}${action.slice(1).replace('_', ' ')} ${label}`
}))
),
...clientPermissions.flatMap(([resource, actions, label]) =>
actions.map(action => ({
resource,
action,
msp: false,
client: true,
description: `${action.charAt(0).toUpperCase()}${action.slice(1).replace('_', ' ')} ${label}`
}))
)
];
for (const { tenant } of tenants) {
const existingPermissions = await knex('permissions').where({ tenant });
const existingPermMap = new Map();
existingPermissions.forEach(p => {
const key = `${p.resource}:${p.action}:${p.msp ? 'msp' : 'client'}`;
existingPermMap.set(key, p);
});
const permissionsToInsert = [];
for (const perm of allPermissions) {
const key = `${perm.resource}:${perm.action}:${perm.msp ? 'msp' : 'client'}`;
if (!existingPermMap.has(key)) {
permissionsToInsert.push({ tenant, ...perm });
}
}
if (permissionsToInsert.length > 0) {
await knex('permissions').insert(permissionsToInsert);
console.log(`Inserted ${permissionsToInsert.length} Algadesk permissions for tenant ${tenant}`);
} else {
console.log(`All Algadesk permissions already exist for tenant ${tenant}`);
}
}
};