Skip to content

Commit

Permalink
refactor: make fields and objects tabs declarative
Browse files Browse the repository at this point in the history
  • Loading branch information
catalandres committed Dec 25, 2023
1 parent e3f5ce4 commit 1bab0b3
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 153 deletions.
168 changes: 15 additions & 153 deletions src/shared/xlsx/ExcelWriter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { mkdir } from 'node:fs/promises';
import * as ExcelJS from 'exceljs';
import { Extended, Album } from '../metadata/file/index.js';
import * as Metadata from '../metadata/types/metadata.js';
import { ALL_TABLES } from './index.js';

export class ExcelWriter {
private album: Album;
Expand All @@ -18,159 +19,20 @@ export class ExcelWriter {
public async writeXlsx(): Promise<string> {
const workbook = new ExcelJS.default.Workbook();

if (this.album.objects?.length > 0) {
const objectWorksheet = workbook.addWorksheet('Objects');
objectWorksheet.columns = [
{ header: 'Name', key: 'name', width: 64 },
{ header: 'Label', key: 'label', width: 64 },
{ header: 'Label (Plural)', key: 'pluralLabel', width: 64 },
{ header: 'Gender', key: 'gender', width: 64 },
{ header: 'Starts With', key: 'startsWith', width: 64 },
{ header: 'Description', key: 'description', width: 128 },
{ header: 'Default Internal Access', key: 'sharingModel', width: 64 },
{ header: 'Default External Access', key: 'externalSharingModel', width: 64 },
{ header: 'Deployment Status', key: 'deploymentStatus', width: 64 },
{ header: 'Activities Enabled', key: 'enableActivities', width: 64 },
{ header: 'Chatter Enabled', key: 'allowInChatterGroups', width: 64 },
{ header: 'Feeds Enabled', key: 'enableFeeds', width: 64 },
{ header: 'History Tracking Enabled', key: 'enableHistory', width: 64 },
{ header: 'Reports Enabled', key: 'enableReports', width: 64 },
{ header: 'Search Enabled', key: 'enableSearch', width: 64 },
{ header: 'Platform Event Type', key: 'eventType', width: 64 },
{ header: 'Platform Event Publish Behavior', key: 'publishBehavior', width: 64 },
{ header: 'Permission Set License Required', key: 'enableLicensing', width: 64 },
];
objectWorksheet.getRow(1).font = { bold: true };
for (const thisObject of this.album.objects as Array<Extended<Metadata.CustomObject>>) {
objectWorksheet.addRow({
name: thisObject.name,
label: thisObject.label,
pluralLabel: thisObject.pluralLabel,
gender: thisObject.gender,
startsWith: thisObject.startsWith,
description: thisObject.description,
sharingModel: thisObject.sharingModel,
externalSharingModel: thisObject.externalSharingModel,
deploymentStatus: thisObject.deploymentStatus,
enableActivities: thisObject.enableActivities,
allowInChatterGroups: thisObject.allowInChatterGroups,
enableFeeds: thisObject.enableFeeds,
enableHistory: thisObject.enableHistory,
enableReports: thisObject.enableReports,
enableSearch: thisObject.enableSearch,
eventType: thisObject.eventType,
publishBehavior: thisObject.publishBehavior,
enableLicensing: thisObject.enableLicensing,
});
}
}

if (this.album.fields?.length > 0) {
const fieldWorksheet = workbook.addWorksheet('Fields');
fieldWorksheet.columns = [
{ header: 'Object', key: 'objectName', width: 64 },
{ header: 'Name', key: 'name', width: 64 },
{ header: 'Full Name', key: 'fullName', width: 64 },
{ header: 'Label', key: 'label', width: 64 },
{ header: 'Type', key: 'type', width: 64 },
{ header: 'Required', key: 'required', width: 64 },
{ header: 'Unique', key: 'unique', width: 64 },
{ header: 'External ID', key: 'externalId', width: 64 },
{ header: 'AI Prediction Field', key: 'isAIPredictionField', width: 64 },
{ header: 'Case Sensitive', key: 'caseSensitive', width: 64 },

{ header: 'Encryption Scheme (Shield)', key: 'encryptionScheme', width: 64 },
{ header: 'Masking Type (Classic)', key: 'maskType', width: 64 },
{ header: 'Masking Character (Classic)', key: 'maskChar', width: 64 },

{ header: 'Description', key: 'description', width: 128 },
{ header: 'Help Text', key: 'inlineHelpText', width: 128 },

{ header: 'Default Value', key: 'defaultValue', width: 64 },
{ header: 'Length', key: 'length', width: 64 },
{ header: 'Visible Lines', key: 'visibleLines', width: 64 },
{ header: 'Precision', key: 'precision', width: 64 },
{ header: 'Scale', key: 'scale', width: 64 },
{ header: 'Value Set', key: 'valueSet', width: 64 },
{ header: 'Formula', key: 'formula', width: 64 },
{ header: 'Treat Formula Blanks As', key: 'formulaTreatBlanksAs', width: 64 },

{ header: 'Reference To', key: 'referenceTo', width: 64 },
{ header: 'Relationship Name', key: 'relationshipName', width: 64 },
{ header: 'Relationship Label', key: 'relationshipLabel', width: 64 },
{ header: 'Relationship Order', key: 'relationshipOrder', width: 64 },
{ header: 'Lookup Filter', key: 'lookupFilter', width: 64 },
{ header: 'Delete Constraint', key: 'deleteConstraint', width: 64 },
{ header: 'Reparentable Master Detail', key: 'reparentableMasterDetail', width: 64 },
{ header: 'Write Requires Master Read', key: 'writeRequiresMasterRead', width: 64 },

{ header: 'Summary Operation', key: 'summaryOperation', width: 64 },
{ header: 'Summarized Field', key: 'summarizedField', width: 64 },
{ header: 'Summary Filter Items', key: 'summaryFilterItems', width: 64 },

{ header: 'Track Feed History', key: 'trackFeedHistory', width: 64 },
{ header: 'Track History', key: 'trackHistory', width: 64 },
{ header: 'Track Trending', key: 'trackTrending', width: 64 },

{ header: 'Security Classification', key: 'securityClassification', width: 64 },
{ header: 'Compliance Group', key: 'complianceGroup', width: 64 },
{ header: 'Business Owner Group', key: 'businessOwnerGroup', width: 64 },
{ header: 'Business Owner User', key: 'businessOwnerUser', width: 64 },
{ header: 'Business Status', key: 'businessStatus', width: 64 },
];
fieldWorksheet.getRow(1).font = { bold: true };
for (const thisField of this.album.fields as Array<Extended<Metadata.CustomField>>) {
fieldWorksheet.addRow({
objectName: thisField.objectName,
fullName: thisField.fullName,
name: thisField.name,
label: thisField.label,
type: thisField.type,
required: thisField.required,
unique: thisField.unique,
externalId: thisField.externalId,
isAIPredictionField: thisField.isAIPredictionField,
caseSensitive: thisField.caseSensitive,

encryptionScheme: thisField.encryptionScheme,
maskType: thisField.maskType,
maskChar: thisField.maskChar,

description: thisField.description,
inlineHelpText: thisField.inlineHelpText,

defaultValue: thisField.defaultValue,
length: thisField.length,
visibleLines: thisField.visibleLines,
precision: thisField.precision,
scale: thisField.scale,
valueSet: thisField.valueSet,
formula: thisField.formula,
formulaTreatBlanksAs: thisField.formulaTreatBlanksAs,

referenceTo: thisField.referenceTo,
relationshipName: thisField.relationshipName,
relationshipLabel: thisField.relationshipLabel,
relationshipOrder: thisField.relationshipOrder,
lookupFilter: thisField.lookupFilter,
deleteConstraint: thisField.deleteConstraint,
reparentableMasterDetail: thisField.reparentableMasterDetail,
writeRequiresMasterRead: thisField.writeRequiresMasterRead,

summaryOperation: thisField.summaryOperation,
summarizedField: thisField.summarizedField,
summaryFilterItems: thisField.summaryFilterItems,

trackFeedHistory: thisField.trackFeedHistory,
trackHistory: thisField.trackHistory,
trackTrending: thisField.trackTrending,

securityClassification: thisField.securityClassification,
complianceGroup: thisField.complianceGroup,
businessOwnerGroup: thisField.businessOwnerGroup,
businessOwnerUser: thisField.businessOwnerUser,
businessStatus: thisField.businessStatus,
});
for (const thisTable of ALL_TABLES) {
if (this.album[thisTable.list]?.length > 0) {
const worksheet = workbook.addWorksheet(thisTable.name);
worksheet.columns = thisTable.columns.map((column) => ({ header: column.label, key: column.field, width: 64 }));
worksheet.getRow(1).font = { bold: true };
for (const thisRecord of this.album[thisTable.list] as Array<
Extended<typeof thisTable.definition.metadataType>
>) {
const row: Record<string, unknown> = {};
for (const thisColumn of thisTable.columns) {
row[thisColumn.field] = thisRecord[thisColumn.field];
}
worksheet.addRow(row);
}
}
}

Expand Down
13 changes: 13 additions & 0 deletions src/shared/xlsx/classes/Table.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Definition } from '../../metadata/file/classes/Definition.js';

interface TableColumn {
readonly label: string;
readonly field: string;
}

export interface Table {
readonly name: string;
readonly list: string;
readonly definition: Definition;
readonly columns: TableColumn[];
}
1 change: 1 addition & 0 deletions src/shared/xlsx/classes/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './Table.js';
3 changes: 3 additions & 0 deletions src/shared/xlsx/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import * as Table from './tables/index.js';

export const ALL_TABLES = [Table.OBJECTS, Table.FIELDS];
52 changes: 52 additions & 0 deletions src/shared/xlsx/tables/Fields.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { Table } from '../classes/Table.js';
import { CUSTOM_FIELD } from '../../metadata/file/index.js';

export const FIELDS: Table = {
name: 'Fields',
list: 'fields',
definition: CUSTOM_FIELD,
columns: [
{ label: 'Object', field: 'objectName' },
{ label: 'Name', field: 'name' },
{ label: 'Full Name', field: 'fullName' },
{ label: 'Label', field: 'label' },
{ label: 'Type', field: 'type' },
{ label: 'Required', field: 'required' },
{ label: 'Unique', field: 'unique' },
{ label: 'External ID', field: 'externalId' },
{ label: 'AI Prediction Field', field: 'isAIPredictionField' },
{ label: 'Case Sensitive', field: 'caseSensitive' },
{ label: 'Encryption Scheme (Shield)', field: 'encryptionScheme' },
{ label: 'Masking Type (Classic)', field: 'maskType' },
{ label: 'Masking Character (Classic)', field: 'maskChar' },
{ label: 'Description', field: 'description' },
{ label: 'Help Text', field: 'inlineHelpText' },
{ label: 'Default Value', field: 'defaultValue' },
{ label: 'Length', field: 'length' },
{ label: 'Visible Lines', field: 'visibleLines' },
{ label: 'Precision', field: 'precision' },
{ label: 'Scale', field: 'scale' },
{ label: 'Value Set', field: 'valueSet' },
{ label: 'Formula', field: 'formula' },
{ label: 'Treat Formula Blanks As', field: 'formulaTreatBlanksAs' },
{ label: 'Reference To', field: 'referenceTo' },
{ label: 'Relationship Name', field: 'relationshipName' },
{ label: 'Relationship Label', field: 'relationshipLabel' },
{ label: 'Relationship Order', field: 'relationshipOrder' },
{ label: 'Lookup Filter', field: 'lookupFilter' },
{ label: 'Delete Constraint', field: 'deleteConstraint' },
{ label: 'Reparentable Master Detail', field: 'reparentableMasterDetail' },
{ label: 'Write Requires Master Read', field: 'writeRequiresMasterRead' },
{ label: 'Summary Operation', field: 'summaryOperation' },
{ label: 'Summarized Field', field: 'summarizedField' },
{ label: 'Summary Filter Items', field: 'summaryFilterItems' },
{ label: 'Track Feed History', field: 'trackFeedHistory' },
{ label: 'Track History', field: 'trackHistory' },
{ label: 'Track Trending', field: 'trackTrending' },
{ label: 'Security Classification', field: 'securityClassification' },
{ label: 'Compliance Group', field: 'complianceGroup' },
{ label: 'Business Owner Group', field: 'businessOwnerGroup' },
{ label: 'Business Owner User', field: 'businessOwnerUser' },
{ label: 'Business Status', field: 'businessStatus' },
],
};
28 changes: 28 additions & 0 deletions src/shared/xlsx/tables/Objects.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Table } from '../classes/Table.js';
import { CUSTOM_OBJECT } from '../../metadata/file/index.js';

export const OBJECTS: Table = {
name: 'Objects',
list: 'objects',
definition: CUSTOM_OBJECT,
columns: [
{ label: 'Name', field: 'name' },
{ label: 'Label', field: 'label' },
{ label: 'Label (Plural)', field: 'pluralLabel' },
{ label: 'Gender', field: 'gender' },
{ label: 'Starts with', field: 'startsWith' },
{ label: 'Description', field: 'description' },
{ label: 'Default Internal Access', field: 'sharingModel' },
{ label: 'Default External Access', field: 'externalSharingModel' },
{ label: 'Deployment Status', field: 'deploymentStatus' },
{ label: 'Activities Enabled', field: 'enableActivities' },
{ label: 'Chatter Enabled', field: 'allowInChatterGroups' },
{ label: 'Feeds Enabled', field: 'enableFeeds' },
{ label: 'History Tracking Enabled', field: 'enableHistory' },
{ label: 'Reports Enabled', field: 'enableReports' },
{ label: 'Search Enabled', field: 'enableSearch' },
{ label: 'Platform Event Type', field: 'eventType' },
{ label: 'Platform Event Publish Behavior', field: 'publishBehavior' },
{ label: 'Permission Set License Required', field: 'enableLicensing' },
],
};
2 changes: 2 additions & 0 deletions src/shared/xlsx/tables/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './Objects.js';
export * from './Fields.js';

0 comments on commit 1bab0b3

Please sign in to comment.