Skip to content

Commit

Permalink
Fix profile order.
Browse files Browse the repository at this point in the history
  • Loading branch information
robgietema committed Oct 3, 2023
1 parent b141b94 commit 700ff1d
Show file tree
Hide file tree
Showing 12 changed files with 397 additions and 426 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
- Apply behaviors to children in get content call @robgietema
- Reindex parent after delete @robgietema
- Fix addable in types endpoint @robgietema
- Fix import order of profiles @robgietema

### Internal

Expand Down
32 changes: 15 additions & 17 deletions src/seeds/001_permission.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,27 @@
import { map } from 'lodash';

import { fileExists, log, stripI18n } from '../helpers';
import { fileExists, log, mapAsync, stripI18n } from '../helpers';
import { Permission } from '../models';

const { config } = require(`${process.cwd()}/config`);

export const seed = async (knex) => {
try {
await Promise.all(
map(config.profiles, async (profilePath) => {
if (fileExists(`${profilePath}/permissions`)) {
const profile = stripI18n(require(`${profilePath}/permissions`));
if (profile.purge) {
await Permission.delete(knex);
}
await Promise.all(
map(
profile.permissions,
async (permission) =>
await Permission.create(permission, {}, knex),
),
);
await mapAsync(config.profiles, async (profilePath) => {
if (fileExists(`${profilePath}/permissions`)) {
const profile = stripI18n(require(`${profilePath}/permissions`));
if (profile.purge) {
await Permission.delete(knex);
}
}),
);
await Promise.all(
map(
profile.permissions,
async (permission) =>
await Permission.create(permission, {}, knex),
),
);
}
});
log.info('Permissions imported');
} catch (err) {
log.error(err);
Expand Down
38 changes: 18 additions & 20 deletions src/seeds/002_role.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,25 @@ const { config } = require(`${process.cwd()}/config`);

export const seed = async (knex) => {
try {
await Promise.all(
map(config.profiles, async (profilePath) => {
if (fileExists(`${profilePath}/roles`)) {
const profile = stripI18n(require(`${profilePath}/roles`));
if (profile.purge) {
await Role.delete(knex);
}
await mapAsync(profile.roles, async (role, index) => {
await Role.create(
{
...omit(role, ['permissions']),
_permissions: role.permissions,
order: role.order || index,
},
{},
knex,
);
});
await mapAsync(config.profiles, async (profilePath) => {
if (fileExists(`${profilePath}/roles`)) {
const profile = stripI18n(require(`${profilePath}/roles`));
if (profile.purge) {
await Role.delete(knex);
}
}),
);
await mapAsync(profile.roles, async (role, index) => {
await Role.create(
{
...omit(role, ['permissions']),
_permissions: role.permissions,
order: role.order || index,
},
{},
knex,
);
});
}
});
log.info('Roles imported');
} catch (err) {
log.error(err);
Expand Down
42 changes: 20 additions & 22 deletions src/seeds/003_group.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,32 @@
import { map, omit } from 'lodash';

import { fileExists, log, stripI18n } from '../helpers';
import { fileExists, log, mapAsync, stripI18n } from '../helpers';
import { Group } from '../models';

const { config } = require(`${process.cwd()}/config`);

export const seed = async (knex) => {
try {
await Promise.all(
map(config.profiles, async (profilePath) => {
if (fileExists(`${profilePath}/groups`)) {
const profile = stripI18n(require(`${profilePath}/groups`));
if (profile.purge) {
await Group.delete(knex);
}
await Promise.all(
map(profile.groups, async (group) => {
await Group.create(
{
...omit(group, ['roles']),
_roles: group.roles,
},
{},
knex,
);
}),
);
await mapAsync(config.profiles, async (profilePath) => {
if (fileExists(`${profilePath}/groups`)) {
const profile = stripI18n(require(`${profilePath}/groups`));
if (profile.purge) {
await Group.delete(knex);
}
}),
);
await Promise.all(
map(profile.groups, async (group) => {
await Group.create(
{
...omit(group, ['roles']),
_roles: group.roles,
},
{},
knex,
);
}),
);
}
});
log.info('Groups imported');
} catch (err) {
log.error(err);
Expand Down
48 changes: 23 additions & 25 deletions src/seeds/004_user.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,36 @@
import { map, omit } from 'lodash';
import bcrypt from 'bcrypt-promise';

import { fileExists, log, stripI18n } from '../helpers';
import { fileExists, log, mapAsync, stripI18n } from '../helpers';
import { User } from '../models';

const { config } = require(`${process.cwd()}/config`);

export const seed = async (knex) => {
try {
await Promise.all(
map(config.profiles, async (profilePath) => {
if (fileExists(`${profilePath}/users`)) {
const profile = stripI18n(require(`${profilePath}/users`));
if (profile.purge) {
await User.delete(knex);
}
await Promise.all(
map(profile.users, async (user) => {
// Insert user
await User.create(
{
...omit(user, ['password', 'roles', 'groups']),
password: await bcrypt.hash(user.password, 10),
_roles: user.roles,
_groups: user.groups,
},
{},
knex,
);
}),
);
await mapAsync(config.profiles, async (profilePath) => {
if (fileExists(`${profilePath}/users`)) {
const profile = stripI18n(require(`${profilePath}/users`));
if (profile.purge) {
await User.delete(knex);
}
}),
);
await Promise.all(
map(profile.users, async (user) => {
// Insert user
await User.create(
{
...omit(user, ['password', 'roles', 'groups']),
password: await bcrypt.hash(user.password, 10),
_roles: user.roles,
_groups: user.groups,
},
{},
knex,
);
}),
);
}
});
log.info('Users imported');
} catch (err) {
log.error(err);
Expand Down
32 changes: 15 additions & 17 deletions src/seeds/005_workflow.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,27 @@
import { map } from 'lodash';

import { fileExists, log, stripI18n } from '../helpers';
import { fileExists, log, mapAsync, stripI18n } from '../helpers';
import { Workflow } from '../models';

const { config } = require(`${process.cwd()}/config`);

export const seed = async (knex) => {
try {
await Promise.all(
map(config.profiles, async (profilePath) => {
if (fileExists(`${profilePath}/workflows`)) {
const profile = stripI18n(require(`${profilePath}/workflows`));
if (profile.purge) {
await Workflow.delete(knex);
}
await Promise.all(
map(
profile.workflows,
async (workflow) => await Workflow.create(workflow, {}, knex),
),
);
log.info('Workflows imported');
await mapAsync(config.profiles, async (profilePath) => {
if (fileExists(`${profilePath}/workflows`)) {
const profile = stripI18n(require(`${profilePath}/workflows`));
if (profile.purge) {
await Workflow.delete(knex);
}
}),
);
await Promise.all(
map(
profile.workflows,
async (workflow) => await Workflow.create(workflow, {}, knex),
),
);
log.info('Workflows imported');
}
});
} catch (err) {
log.error(err);
}
Expand Down
70 changes: 33 additions & 37 deletions src/seeds/006_type.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,47 +8,43 @@ const { config } = require(`${process.cwd()}/config`);

export const seed = async (knex) => {
try {
await Promise.all(
map(config.profiles, async (profilePath) => {
if (dirExists(`${profilePath}/behaviors`)) {
// Get behavior profiles
const behaviors = map(
await fs.readdir(`${profilePath}/behaviors`),
(file) => dropRight(file.split('.')).join('.'),
).sort();
await mapAsync(config.profiles, async (profilePath) => {
if (dirExists(`${profilePath}/behaviors`)) {
// Get behavior profiles
const behaviors = map(
await fs.readdir(`${profilePath}/behaviors`),
(file) => dropRight(file.split('.')).join('.'),
).sort();

// Import behaviors
await mapAsync(behaviors, async (behavior) => {
const data = stripI18n(
require(`${profilePath}/behaviors/${behavior}`),
);
await Behavior.create(data, {}, knex);
});
}
}),
);
// Import behaviors
await mapAsync(behaviors, async (behavior) => {
const data = stripI18n(
require(`${profilePath}/behaviors/${behavior}`),
);
await Behavior.create(data, {}, knex);
});
}
});

await Promise.all(
map(config.profiles, async (profilePath) => {
if (dirExists(`${profilePath}/types`)) {
// Get type profiles
const types = map(await fs.readdir(`${profilePath}/types`), (file) =>
dropRight(file.split('.')).join('.'),
).sort();
await mapAsync(config.profiles, async (profilePath) => {
if (dirExists(`${profilePath}/types`)) {
// Get type profiles
const types = map(await fs.readdir(`${profilePath}/types`), (file) =>
dropRight(file.split('.')).join('.'),
).sort();

// Import types
await mapAsync(types, async (type) => {
const data = stripI18n(require(`${profilePath}/types/${type}`));
const typeModel = await Type.create({
global_allow: true,
filter_content_types: false,
...data,
});
await typeModel.cacheSchema();
// Import types
await mapAsync(types, async (type) => {
const data = stripI18n(require(`${profilePath}/types/${type}`));
const typeModel = await Type.create({
global_allow: true,
filter_content_types: false,
...data,
});
}
}),
);
await typeModel.cacheSchema();
});
}
});
log.info('Types imported');
} catch (err) {
log.error(err);
Expand Down
Loading

0 comments on commit 700ff1d

Please sign in to comment.