Skip to content
This repository has been archived by the owner on Aug 11, 2024. It is now read-only.

Commit

Permalink
Merge pull request #274 from beabee-communityrm/feat/easier-onboarding
Browse files Browse the repository at this point in the history
feat: new and update tools for a simpler setup process
  • Loading branch information
wpf500 authored Jun 21, 2023
2 parents 31207db + 3a6e0f5 commit 1255a13
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 16 deletions.
52 changes: 52 additions & 0 deletions src/tools/configure.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import "module-alias/register";

import inquirer, { QuestionCollection } from "inquirer";
import { getRepository } from "typeorm";

import * as db from "@core/database";

import OptionsService from "@core/services/OptionsService";

import Content from "@models/Content";

function notEmpty(msg: string) {
return (s: string) => {
return s.trim() === "" ? msg : true;
};
}

const questions: QuestionCollection[] = [];

questions.push({
type: "input",
name: "emailDomain",
message: "Email Domain",
validate: notEmpty("You must enter an email domain")
});

questions.push({
type: "checkbox",
name: "paymentProviders",
message: "Payment Methods",
choices: [
{ name: "Credit card (Stripe)", value: "s_card" },
{ name: "SEPA direct debit (Stripe)", value: "s_sepa" },
{ name: "BACS (Stripe)", value: "s_bacs" },
{ name: "Direct debit (GoCardless)", value: "gc_direct-debit" }
]
});

db.connect().then(async () => {
const answers = await inquirer.prompt(questions);

await OptionsService.set("support-email", "support@" + answers.emailDomain);

await getRepository(Content).update("join", {
data: () =>
`jsonb_set(data, \'{paymentMethods}\', \'${JSON.stringify(
answers.paymentProviders
)}\')`
});

await db.close();
});
45 changes: 29 additions & 16 deletions src/tools/new-user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ import { generatePassword, passwordRequirements } from "@core/utils/auth";
import ContactsService from "@core/services/ContactsService";

import ContactRole from "@models/ContactRole";
import ResetPasswordFlow from "@models/ResetPasswordFlow";

import config from "@config";

function notEmpty(msg: string) {
return (s: string) => {
return s.trim() === "" ? msg : true;
};
}

const questions: QuestionCollection[] = [];

Expand All @@ -19,38 +28,32 @@ questions.push({
type: "input",
name: "firstname",
message: "First Name",
validate: function (s) {
return s.trim() === "" ? "You must enter a first name" : true;
}
validate: notEmpty("You must enter a first name")
});

// Last Name
questions.push({
type: "input",
name: "lastname",
message: "Last Name",
validate: function (s) {
return s.trim() === "" ? "You must enter a last name" : true;
}
validate: notEmpty("You must enter a last name")
});

// Email address
questions.push({
type: "input",
name: "email",
message: "Email Address",
validate: function (s) {
return s.trim() === "" ? "You must enter an email address" : true;
}
validate: notEmpty("You must enter an email address")
});

// Password
questions.push({
type: "password",
name: "password",
message: "Password",
validate: function (s) {
return passwordRequirements(s);
message: "Password (leave empty to generate reset password link)",
validate: (s) => {
return !s.trim() || passwordRequirements(s);
}
});

Expand Down Expand Up @@ -80,8 +83,6 @@ questions.push({
db.connect().then(async () => {
const answers = await inquirer.prompt(questions);

const password = await generatePassword(answers.password);

const roles = [];

if (answers.membership != "No") {
Expand Down Expand Up @@ -113,14 +114,26 @@ db.connect().then(async () => {
roles.push(admin);
}

await ContactsService.createContact({
const contact = await ContactsService.createContact({
firstname: answers.firstname,
lastname: answers.lastname,
email: answers.email,
contributionType: ContributionType.None,
roles: roles,
password
...(answers.password && {
password: await generatePassword(answers.password)
})
});

if (!answers.password) {
const rpFlow = await getRepository(ResetPasswordFlow).save({
contact
});

console.log(
`Reset password link: ${config.audience}/auth/set-password/${rpFlow.id}`
);
}

await db.close();
});

0 comments on commit 1255a13

Please sign in to comment.