Skip to content

Commit

Permalink
Merge pull request #19 from AplinkosMinisterija/fix-seeds
Browse files Browse the repository at this point in the history
Fixing seeds - double check before creating
  • Loading branch information
ambrazasp committed Mar 25, 2024
2 parents 9712484 + adb330e commit 756d501
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 30 deletions.
7 changes: 6 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,9 @@ POSTMARK_PASSWORD_RESET_TEMPLATE_ID=

RECAPTCHA_SECRET=secret

SENTRY_DSN=
SENTRY_DSN=

DEFAULT_SUPER_ADMIN_EMAIL=superadmin@biip.lt
DEFAULT_SUPER_ADMIN_PASSWORD=Slaptazodis1@
DEFAULT_ADMIN_APP_URL=https://admin.biip.lt
DEFAULT_PROJECT_NAME_GENITIVE=BĮIP
82 changes: 53 additions & 29 deletions services/seed.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,58 @@ import { AppType } from './apps.service';
export default class SeedService extends moleculer.Service {
@Action()
async real(ctx: Context<Record<string, unknown>>) {
// create apps
await ctx.call('apps.create', {
name: 'Admin',
type: AppType.ADMIN,
url: 'https://admin.biip.lt',
settings: {
productNameTo: 'BĮIP administravimo sistemos',
},
});

await ctx.call('apps.create', {
name: 'Vidiniai naudotojai',
type: AppType.USERS,
url: 'https://admin.biip.lt',
settings: {
productNameTo: 'BĮĮP naudotojų valdymo sistemos',
},
});
const adminEmail = process.env.DEFAULT_SUPER_ADMIN_EMAIL || 'superadmin@am.lt';
const adminPassword = process.env.DEFAULT_SUPER_ADMIN_PASSWORD || 'Slaptazodis1@';
const adminAppUrl = process.env.DEFAULT_ADMIN_APP_URL || 'https://admin.biip.lt';
const projectNameGenitive = process.env.DEFAULT_PROJECT_NAME_GENITIVE || 'BĮIP';
const adminAppExists = await ctx.call('apps.count', { query: { type: AppType.ADMIN } });
const usersAppExists = await ctx.call('apps.count', { query: { type: AppType.USERS } });
const userExists = await ctx.call('users.count', { query: { email: adminEmail } });

// create local user
await ctx.call('usersLocal.invite', {
email: 'superadmin@am.lt',
firstName: 'Super',
lastName: 'Admin',
password: 'Slaptazodis1@',
phone: '+37060000000',
type: UserType.SUPER_ADMIN,
doNotSendEmail: true,
});
if (!adminAppExists) {
// create admin app
await ctx.call('apps.create', {
name: 'Admin',
type: AppType.ADMIN,
url: adminAppUrl,
settings: {
productNameTo: `${projectNameGenitive} administravimo sistemos`,
},
});
}

if (!usersAppExists) {
// create users app
await ctx.call('apps.create', {
name: 'Vidiniai naudotojai',
type: AppType.USERS,
url: adminAppUrl,
settings: {
productNameTo: `${projectNameGenitive} naudotojų valdymo sistemos`,
},
});
}

if (!userExists) {
// create super admin user
await ctx.call(
'usersLocal.invite',
{
email: adminEmail,
firstName: 'Super',
lastName: 'Admin',
password: adminPassword,
phone: '+37060000000',
type: UserType.SUPER_ADMIN,
doNotSendEmail: true,
},
{
meta: {
hasPermissions: true,
},
},
);
}

return true;
}
Expand All @@ -58,8 +81,9 @@ export default class SeedService extends moleculer.Service {
.waitForServices(['users', 'usersLocal', 'apps', 'groups', 'permissions'])
.then(async () => {
const usersCount: number = await this.broker.call('users.count');
const appsCount: number = await this.broker.call('apps.count');

if (usersCount === 0) {
if (!usersCount || !appsCount) {
await this.broker.call('seed.real', {}, { timeout: 120 * 1000 });
}
});
Expand Down

0 comments on commit 756d501

Please sign in to comment.