forked from ytgov/internal-data-portal
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #99 from icefoganalytics/issue-37/automate-dataset…
…-field-creation-from-api-link Automate Dataset Field Creation From API Link
- Loading branch information
Showing
12 changed files
with
290 additions
and
51 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
api/src/services/dataset-integrations/bulk-replace-dataset-fields-service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { isEmpty, isNil, isNumber, startCase } from "lodash" | ||
import { CreationAttributes } from "sequelize" | ||
|
||
import db, { DatasetField, DatasetIntegration } from "@/models" | ||
import { DatasetFieldDataTypes } from "@/models/dataset-field" | ||
import BaseService from "@/services/base-service" | ||
|
||
export class BulkReplaceDatasetFieldsService extends BaseService { | ||
constructor(private datasetIntegration: DatasetIntegration) { | ||
super() | ||
} | ||
|
||
async perform(): Promise<DatasetField[]> { | ||
const { datasetId, parsedJsonData } = this.datasetIntegration | ||
|
||
return db.transaction(async () => { | ||
await DatasetField.destroy({ | ||
where: { | ||
datasetId, | ||
}, | ||
}) | ||
|
||
if (isNil(parsedJsonData) || isEmpty(parsedJsonData)) { | ||
throw new Error("An integration must have data to build dataset fields.") | ||
} | ||
|
||
const firstRecord = parsedJsonData[0] | ||
const datasetFieldsAttributes: CreationAttributes<DatasetField>[] = Object.entries( | ||
firstRecord | ||
).map(([key, value]) => { | ||
const displayName = startCase(key) | ||
const dataType = this.determineDataType(value) | ||
return { | ||
datasetId, | ||
name: key, | ||
displayName, | ||
dataType, | ||
} | ||
}) | ||
return DatasetField.bulkCreate(datasetFieldsAttributes) | ||
}) | ||
} | ||
|
||
private determineDataType(value: unknown): DatasetFieldDataTypes { | ||
if (isNumber(value)) { | ||
return DatasetField.DataTypes.INTEGER | ||
} | ||
|
||
return DatasetField.DataTypes.TEXT | ||
} | ||
} | ||
|
||
export default BulkReplaceDatasetFieldsService |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { isArray, isString } from "lodash" | ||
|
||
export function isStringArray<T>(data: T | string[]): data is string[] { | ||
return isArray(data) && data.every(isString) | ||
} | ||
|
||
export default isStringArray |
Oops, something went wrong.