The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
import { Injectable } from '@nestjs/common';
import { ALL_FLAT_ENTITY_MAPS_PROPERTIES } from 'src/engine/metadata-modules/flat-entity/constant/all-flat-entity-maps-properties.constant';
import { AllFlatEntityMaps } from 'src/engine/metadata-modules/flat-entity/types/all-flat-entity-maps.type';
import { withDerivedFieldMetadataMaps } from 'src/engine/metadata-modules/flat-entity/utils/with-derived-field-metadata-maps.util';
import { WorkspaceCacheService } from 'src/engine/workspace-cache/services/workspace-cache.service';
import {
type WorkspaceCacheDataMap,
type WorkspaceCacheResultWithHashes,
} from 'src/engine/workspace-cache/types/workspace-cache-key.type';
export type FlatEntityMapsCacheKeyName =
| keyof AllFlatEntityMaps
| 'flatApplicationMaps'
| 'flatFieldMetadataMapsOrm';
@Injectable()
export class WorkspaceManyOrAllFlatEntityMapsCacheService {
constructor(private readonly workspaceCacheService: WorkspaceCacheService) {}
public async getOrRecomputeManyOrAllFlatEntityMaps<
T extends FlatEntityMapsCacheKeyName[] = (keyof AllFlatEntityMaps)[],
>({
flatMapsKeys,
workspaceId,
}: {
workspaceId: string;
flatMapsKeys?: T;
}): Promise<Pick<WorkspaceCacheDataMap, T[number]>> {
const { data } = await this.getOrRecomputeManyOrAllFlatEntityMapsWithHashes(
{ flatMapsKeys, workspaceId },
);
return data;
}
public async getOrRecomputeManyOrAllFlatEntityMapsWithHashes<
T extends FlatEntityMapsCacheKeyName[] = (keyof AllFlatEntityMaps)[],
>({
flatMapsKeys,
workspaceId,
}: {
workspaceId: string;
flatMapsKeys?: T;
}): Promise<WorkspaceCacheResultWithHashes<T>> {
return await this.workspaceCacheService.getOrRecomputeWithHashes(
workspaceId,
(flatMapsKeys ??
ALL_FLAT_ENTITY_MAPS_PROPERTIES) as (keyof WorkspaceCacheDataMap)[],
);
}
public async invalidateFlatEntityMaps<
T extends FlatEntityMapsCacheKeyName[] = (keyof AllFlatEntityMaps)[],
>({
flatMapsKeys,
workspaceId,
}: {
workspaceId: string;
flatMapsKeys?: T;
}): Promise<void> {
await this.workspaceCacheService.invalidateAndRecompute(
workspaceId,
withDerivedFieldMetadataMaps(
(flatMapsKeys ??
ALL_FLAT_ENTITY_MAPS_PROPERTIES) as (keyof WorkspaceCacheDataMap)[],
),
);
}
public async flushFlatEntityMaps<
T extends FlatEntityMapsCacheKeyName[] = (keyof AllFlatEntityMaps)[],
>({
flatMapsKeys,
workspaceId,
}: {
workspaceId: string;
flatMapsKeys?: T;
}): Promise<void> {
await this.workspaceCacheService.flush(
workspaceId,
withDerivedFieldMetadataMaps(
(flatMapsKeys ??
ALL_FLAT_ENTITY_MAPS_PROPERTIES) as (keyof WorkspaceCacheDataMap)[],
),
);
}
}