feat: Add seed people and companies data for demo environment (#2207) (#2307)

* feat: seed companies and people data

* init DataSeedDemoWorkspaceCommand to handle:
- seedCoreSchema()
- seedMetadataSchema()

* feature: Seed workspace with demo data

- delete workspace
- initDemo() with prefillWorkspaceWithDemoObjects()

* added companies-demo.ts with data
* added people-demo.ts with data

* added workspaceId to seedFeatureFlags()

* delete previous CoreSchema before seedCoreSchema

* added workspaceMemberPrefillData

* getDemoWorkspaces() to get DEMO_WORKSPACES from config

* defined DemoSeedUserIds

- created core/demo/ to keep modified seedCoreSchema() there
- DemoSeedUserIds with new set of users and Ids

* generateOpportunities() to seed demo opportunities (limit = 50)

* Code review and fixes

* Fix

---------

Co-authored-by: Charles Bochet <charles@twenty.com>
This commit is contained in:
Ruslan
2023-12-03 00:37:45 +07:00
committed by GitHub
parent 31f29582d0
commit fd9467c54d
35 changed files with 5190 additions and 38 deletions

View File

@ -0,0 +1,76 @@
import { EntityManager } from 'typeorm';
import { v4 } from 'uuid';
const tableName = 'opportunity';
const getRandomProbability = () => {
const firstDigit = Math.floor(Math.random() * 9) + 1;
return firstDigit / 10;
};
const getRandomPipelineStepId = (pipelineStepIds: { id: string }[]) =>
pipelineStepIds[Math.floor(Math.random() * pipelineStepIds.length)].id;
const generateRandomAmountMicros = () => {
const firstDigit = Math.floor(Math.random() * 9) + 1;
return firstDigit * 10000000000;
};
// Function to generate the array of opportunities
// companiesWithPeople - selecting from the db companies and 1 person related to the company.id to use companyId, pointOfContactId and personId
// pipelineStepIds - selecting from the db pipeline, getting random id from selected to use as pipelineStepId
const generateOpportunities = (
companies,
pipelineStepIds: { id: string }[],
) => {
return companies.map((company) => ({
id: v4(),
amountAmountMicros: generateRandomAmountMicros(),
amountCurrencyCode: 'USD',
closeDate: new Date(),
probability: getRandomProbability(),
pipelineStepId: getRandomPipelineStepId(pipelineStepIds),
pointOfContactId: company.personId,
personId: company.personId,
companyId: company.id,
}));
};
export const seedDemoOpportunity = async (
entityManager: EntityManager,
schemaName: string,
) => {
const companiesWithPeople = await entityManager?.query(
`SELECT company.*, person.id AS "personId"
FROM ${schemaName}.company
LEFT JOIN ${schemaName}.person ON company.id = "person"."companyId"
LIMIT 50`,
);
const pipelineStepIds = await entityManager?.query(
`SELECT id FROM ${schemaName}."pipelineStep"`,
);
const opportunities = generateOpportunities(
companiesWithPeople,
pipelineStepIds,
);
await entityManager
.createQueryBuilder()
.insert()
.into(`${schemaName}.${tableName}`, [
'id',
'amountAmountMicros',
'amountCurrencyCode',
'closeDate',
'probability',
'pipelineStepId',
'pointOfContactId',
'personId',
'companyId',
])
.orIgnore()
.values(opportunities)
.execute();
};