forked from FlatFilers/platform-sdk-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFullExample.ts
130 lines (123 loc) · 2.78 KB
/
FullExample.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/**
* Example workbook for importing employees
* Test with the sample file at examples/sample-uploads/employees.csv
*/
import {
BooleanField,
DateField,
Message,
NumberField,
OptionField,
Portal,
Sheet,
TextField,
Workbook,
LinkedField,
} from '@flatfile/configure'
import { FlatfileRecord, FlatfileRecords } from '@flatfile/hooks'
import fetch from 'node-fetch'
const BaseSheet = new Sheet(
'BaseSheet',
{
firstName: TextField({
primary: true,
}),
middleName: TextField('Middle'),
lastName: TextField(),
email: TextField({
unique: true,
}),
},
{
previewFieldKey: 'email',
}
)
const LinkedSheet = new Sheet('LinkedSheet', {
email: LinkedField({
unique: true,
label: 'Email',
primary: true,
sheet: BaseSheet,
}),
firstName: TextField(),
middleName: TextField('Middle'),
lastName: TextField(),
})
const Employees = new Sheet(
'Employees',
{
firstName: TextField({
label: 'First Name',
required: true,
description: 'Given name',
}),
lastName: TextField({
compute: (v: any) => {
return `Rock`
},
}),
fullName: TextField(),
stillEmployed: BooleanField(),
department: OptionField({
label: 'Department',
options: {
engineering: { label: 'Engineering' },
hr: 'People Ops',
sales: 'Revenue',
},
}),
fromHttp: TextField({ label: 'Set by batchRecordCompute' }),
salary: NumberField({
label: 'Salary',
description: 'Annual Salary in USD',
required: true,
validate: (salary: number) => {
const minSalary = 30_000
if (salary < minSalary) {
return [
new Message(
`${salary} is less than minimum wage ${minSalary}`,
'warn',
'validate'
),
]
}
},
}),
startDate: DateField(),
},
{
allowCustomFields: true,
recordCompute: (record) => {
const fullName = `${record.get('firstName')} ${record.get('lastName')}`
record.set('fullName', fullName)
return record
},
batchRecordsCompute: async (payload: FlatfileRecords<any>) => {
const response = await fetch('https://api.us.flatfile.io/health', {
method: 'GET',
headers: {
Accept: 'application/json',
},
})
const result = (await response.json()) as any
payload.records.map(async (record: FlatfileRecord) => {
record.set('fromHttp', result.info.postgres.status)
})
},
}
)
const EmployeesPortal = new Portal({
name: 'EmployeesPortal',
sheet: 'Employees',
})
export default new Workbook({
name: 'Employees',
namespace: 'employee',
sheets: {
Employees,
BaseSheet,
LinkedSheet,
},
portals: [EmployeesPortal],
})