mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-16 04:40:32 +00:00
fix: one click demo
This commit is contained in:
@@ -3,6 +3,8 @@ import { Service, Inject } from 'typedi';
|
||||
import asyncMiddleware from '@/api/middleware/asyncMiddleware';
|
||||
import BaseController from '@/api/controllers/BaseController';
|
||||
import { OneClickDemoApplication } from '@/services/OneClickDemo/OneClickDemoApplication';
|
||||
import { reset } from 'colorette';
|
||||
import { body } from 'express-validator';
|
||||
|
||||
@Service()
|
||||
export class OneClickDemoController extends BaseController {
|
||||
@@ -16,6 +18,14 @@ export class OneClickDemoController extends BaseController {
|
||||
const router = Router();
|
||||
|
||||
router.post('/one_click', asyncMiddleware(this.oneClickDemo.bind(this)));
|
||||
router.post(
|
||||
'/one_click_signin',
|
||||
[
|
||||
body('demo_id').exists(),
|
||||
],
|
||||
this.validationResult,
|
||||
asyncMiddleware(this.oneClickSignIn.bind(this))
|
||||
);
|
||||
|
||||
return router;
|
||||
}
|
||||
@@ -38,4 +48,26 @@ export class OneClickDemoController extends BaseController {
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {Request} req
|
||||
* @param {Response} res
|
||||
* @param {NextFunction} next
|
||||
*/
|
||||
private async oneClickSignIn(
|
||||
req: Request,
|
||||
res: Response,
|
||||
next: NextFunction
|
||||
) {
|
||||
const { demoId } = this.matchedBodyData(req);
|
||||
|
||||
try {
|
||||
const data = await this.oneClickDemoApp.autoSignIn(demoId);
|
||||
|
||||
return res.status(200).send(data);
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
import { Inject, Service } from 'typedi';
|
||||
import { faker } from '@faker-js/faker';
|
||||
import uniqid from 'uniqid';
|
||||
import AuthenticationApplication from '../Authentication/AuthApplication';
|
||||
import OrganizationService from '../Organization/OrganizationService';
|
||||
import { IAuthSignInPOJO } from '@/interfaces';
|
||||
import { OneClickDemo } from '@/system/models/OneclickDemo';
|
||||
import { SystemUser } from '@/system/models';
|
||||
|
||||
@Service()
|
||||
export class CreateOneClickDemo {
|
||||
@@ -16,17 +19,21 @@ export class CreateOneClickDemo {
|
||||
* Creates one-click demo account.
|
||||
* @returns {Promise<ICreateOneClickDemoPOJO>}
|
||||
*/
|
||||
async createOneClickDemo(): Promise<ICreateOneClickDemoPOJO> {
|
||||
public async createOneClickDemo(): Promise<ICreateOneClickDemoPOJO> {
|
||||
const firstName = faker.person.firstName();
|
||||
const lastName = faker.person.lastName();
|
||||
const email = faker.internet.email();
|
||||
const password = '123123123';
|
||||
const demoId = uniqid();
|
||||
|
||||
await this.authApp.signUp({ firstName, lastName, email, password });
|
||||
|
||||
//
|
||||
const signedIn = await this.authApp.signIn(email, password);
|
||||
const tenantId = signedIn.tenant.id;
|
||||
const userId = signedIn.user.id;
|
||||
|
||||
// Creates a new one-click demo.
|
||||
await OneClickDemo.query().insert({ key: demoId, tenantId, userId });
|
||||
|
||||
const buildJob = await this.organizationService.buildRunJob(
|
||||
tenantId,
|
||||
@@ -40,18 +47,33 @@ export class CreateOneClickDemo {
|
||||
},
|
||||
signedIn.user
|
||||
);
|
||||
return { email, signedIn, buildJob };
|
||||
return { email, demoId, signedIn, buildJob };
|
||||
}
|
||||
|
||||
/**
|
||||
* Sign-in automicatlly using the demo id one creating an account finish.
|
||||
* @param {string} oneClickDemoId -
|
||||
*/
|
||||
async autoSignIn(oneClickDemoId: string) {}
|
||||
async autoSignIn(oneClickDemoId: string) {
|
||||
const foundOneclickDemo = await OneClickDemo.query()
|
||||
.findOne('key', oneClickDemoId)
|
||||
.throwIfNotFound();
|
||||
|
||||
const userId = foundOneclickDemo.userId;
|
||||
const user = await SystemUser.query().findById(userId);
|
||||
|
||||
const email = user.email;
|
||||
const password = '123123123';
|
||||
|
||||
const signedIn = await this.authApp.signIn(email, password);
|
||||
|
||||
return signedIn;
|
||||
}
|
||||
}
|
||||
|
||||
interface ICreateOneClickDemoPOJO {
|
||||
email: string;
|
||||
demoId: string;
|
||||
signedIn: IAuthSignInPOJO;
|
||||
buildJob: any;
|
||||
}
|
||||
|
||||
@@ -1,14 +1,25 @@
|
||||
import { Inject, Service } from "typedi";
|
||||
import { CreateOneClickDemo } from "./CreateOneClickDemo";
|
||||
|
||||
import { Inject, Service } from 'typedi';
|
||||
import { CreateOneClickDemo } from './CreateOneClickDemo';
|
||||
|
||||
@Service()
|
||||
export class OneClickDemoApplication {
|
||||
|
||||
@Inject()
|
||||
private createOneClickDemoService: CreateOneClickDemo;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @returns
|
||||
*/
|
||||
public createOneClick() {
|
||||
return this.createOneClickDemoService.createOneClickDemo();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param oneClickDemoId
|
||||
* @returns
|
||||
*/
|
||||
public autoSignIn(oneClickDemoId: string) {
|
||||
return this.createOneClickDemoService.autoSignIn(oneClickDemoId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
/**
|
||||
* @param { import("knex").Knex } knex
|
||||
* @returns { Promise<void> }
|
||||
*/
|
||||
exports.up = function (knex) {
|
||||
return knex.schema.createTable('oneclick_demos', (table) => {
|
||||
table.increments('id');
|
||||
table.string('key');
|
||||
table.integer('tenant_id').unsigned();
|
||||
table.integer('user_id').unsigned();
|
||||
table.timestamps();
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* @param { import("knex").Knex } knex
|
||||
* @returns { Promise<void> }
|
||||
*/
|
||||
exports.down = function (knex) {
|
||||
return knex.schema.dropTableIfExists('oneclick_demos');
|
||||
};
|
||||
17
packages/server/src/system/models/OneclickDemo.ts
Normal file
17
packages/server/src/system/models/OneclickDemo.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import SystemModel from '@/system/models/SystemModel';
|
||||
|
||||
export class OneClickDemo extends SystemModel {
|
||||
/**
|
||||
* Table name
|
||||
*/
|
||||
static get tableName() {
|
||||
return 'oneclick_demos';
|
||||
}
|
||||
|
||||
/**
|
||||
* Timestamps columns.
|
||||
*/
|
||||
get timestamps() {
|
||||
return ['createdAt'];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user