Added random data for the database for the TagType.

This commit is contained in:
samuelmbabhazi
2025-01-09 10:35:13 +02:00
parent eb2b8a88b9
commit ac7da6ac36
3 changed files with 65 additions and 2 deletions
+2 -2
View File
@@ -56,7 +56,7 @@ PLATFORM_WEBSITE_DOWNLOAD_URL=https://gauzy.co/downloads
DB_ORM=typeorm
# DB_TYPE: sqlite | postgres | better-sqlite3 | mysql
DB_TYPE=better-sqlite3
DB_TYPE=postgres
DB_SYNCHRONIZE=false
# DB Connection Parameters
@@ -66,7 +66,7 @@ DB_PORT=5432
DB_NAME=gauzy
## DB Username. The default for PostgreSQL is 'postgres', for MySQL it's 'root'
DB_USER=postgres
DB_PASS=root
DB_PASS=postgres
DB_LOGGING=all
DB_POOL_SIZE=40
DB_POOL_SIZE_KNEX=10
@@ -210,6 +210,7 @@ import { createDefaultPriorities } from './../../tasks/priorities/priority.seed'
import { createDefaultSizes } from './../../tasks/sizes/size.seed';
import { createDefaultIssueTypes } from './../../tasks/issue-type/issue-type.seed';
import { getDBType } from './../../core/utils';
import { createRandomOrganizationTagTypes, createTagTypes } from '../../tag-type/tag-type.seed';
export enum SeederTypeEnum {
ALL = 'all',
@@ -574,6 +575,8 @@ export class SeedDataService {
await this.tryExecute('Default Tags', createDefaultTags(this.dataSource, this.tenant, this.organizations));
await this.tryExecute('Default Tag Types', createTagTypes(this.dataSource, this.tenant, this.organizations));
// Organization level inserts which need connection, tenant, role, organizations
const categories = await this.tryExecute(
'Default Expense Categories',
@@ -1005,6 +1008,11 @@ export class SeedDataService {
createRandomOrganizationTags(this.dataSource, this.randomTenants, this.randomTenantOrganizationsMap)
);
await this.tryExecute(
'Random Organization Tag Types',
createRandomOrganizationTagTypes(this.dataSource, this.randomTenants, this.randomTenantOrganizationsMap)
);
await this.tryExecute(
'Random Organization Documents',
createRandomOrganizationDocuments(this.dataSource, this.randomTenants, this.randomTenantOrganizationsMap)
@@ -55,6 +55,61 @@ export const createTagTypes = async (
return insertTagTypes(dataSource, tagTypes);
};
/**
* Creates random organization tag types for given tenants and their organizations.
*
* @function createRandomOrganizationTagTypes
* @async
* @param {DataSource} dataSource - The TypeORM `DataSource` instance used for database operations.
* @param {ITenant[]} tenants - An array of tenant entities for which random tag types are being created.
* @param {Map<ITenant, IOrganization[]>} tenantOrganizationsMap - A map linking each tenant to its associated organizations.
* @returns {Promise<ITagType[]>} - A promise that resolves to an array of created and saved `ITagType` entities.
*
* @description
* This function generates random tag types for multiple tenants and their organizations.
* For each tenant, it retrieves the associated organizations from the `tenantOrganizationsMap`.
* It iterates over the organizations and creates `TagType` entities based on predefined
* `DEFAULT_TAG_TYPES`. The generated entities are saved in bulk into the database.
*
* If a tenant does not have any organizations, the function logs a warning and skips the tenant.
*
* @throws Will throw an error if the database save operation fails.
*/
export const createRandomOrganizationTagTypes = async (
dataSource: DataSource,
tenants: ITenant[],
tenantOrganizationsMap: Map<ITenant, IOrganization[]>
): Promise<ITagType[]> => {
let tagTypes: TagType[] = [];
for (const tenant of tenants) {
// Fetch organizations for the current tenant
const organizations = tenantOrganizationsMap.get(tenant);
if (!organizations || organizations.length === 0) {
console.warn(`No organizations found for tenant ID: ${tenant.id}`);
continue; // Skip to the next tenant if no organizations are found
}
for (const organization of organizations) {
// Create TagType instances for the current organization
const organizationTagTypes: TagType[] = DEFAULT_TAG_TYPES.map(({ type }) => {
const tagType = new TagType();
tagType.type = type;
tagType.organization = organization;
tagType.tenantId = tenant.id;
return tagType;
});
// Add the new TagType entities to the tagTypes array
tagTypes.push(...organizationTagTypes);
}
}
// Bulk save all created tag types into the database
return await dataSource.manager.save(tagTypes);
};
/**
* Inserts an array of tag types into the database.
*