Files
twenty/packages/twenty-server/src/engine/workspace-manager/demo-objects-prefill-data/opportunity.ts
Charles Bochet 3fa8c4bace Add KanbanFieldMetadataId on View standard object (#4604)
* Add KanbanFieldMetadataId on View standard object

* Deprecate Pipeline step

* Fix

* Use Constants instead of raw ids

* Fix

* Fix query runner

* Fix according to review

* Fix tests

* Fix tests

* Fix tests
2024-03-21 18:08:27 +01:00

73 lines
1.7 KiB
TypeScript

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 getRandomStage = () => {
const stages = ['NEW', 'SCREENING', 'MEETING', 'PROPOSAL', 'CUSTOMER'];
return stages[Math.floor(Math.random() * stages.length)];
};
const generateRandomAmountMicros = () => {
const firstDigit = Math.floor(Math.random() * 9) + 1;
return firstDigit * 10000000000;
};
const generateOpportunities = (companies) => {
return companies.map((company) => ({
id: v4(),
amountAmountMicros: generateRandomAmountMicros(),
amountCurrencyCode: 'USD',
closeDate: new Date(),
stage: getRandomStage(),
probability: getRandomProbability(),
pointOfContactId: company.personId,
companyId: company.id,
}));
};
export const opportunityPrefillDemoData = 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 opportunities = generateOpportunities(companiesWithPeople);
await entityManager
.createQueryBuilder()
.insert()
.into(`${schemaName}.${tableName}`, [
'id',
'amountAmountMicros',
'amountCurrencyCode',
'closeDate',
'stage',
'probability',
'pointOfContactId',
'companyId',
'position',
])
.orIgnore()
.values(
opportunities.map((opportunity, index) => ({
...opportunity,
position: index,
})),
)
.execute();
};