Skip to content

Commit

Permalink
chore: checkin
Browse files Browse the repository at this point in the history
  • Loading branch information
tataihono committed Jan 8, 2025
1 parent 2546331 commit ca7f73b
Show file tree
Hide file tree
Showing 33 changed files with 10,150 additions and 4,607 deletions.
10 changes: 5 additions & 5 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"folder-path-color.folders": [
{ "path": "src/extract", "color": "blue" },
{ "path": "src/load", "color": "magenta" },
{ "path": "src/transform", "color": "cyan" }
]
// "folder-path-color.folders": [
// { "path": "src/extract", "color": "blue" },
// { "path": "src/load", "color": "magenta" },
// { "path": "src/transform", "color": "cyan" }
// ]
}
32 changes: 32 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@
"dependencies": {
"ansi-colors": "^4.1.3",
"axios": "^1.6.2",
"axios-retry": "^4.5.0",
"cli-progress": "^3.12.0",
"dotenv": "^16.3.1",
"fetch-retry": "^6.0.0",
"lodash": "^4.17.21",
"openapi-fetch": "^0.8.2",
"zod": "^3.24.1"
Expand Down
5 changes: 3 additions & 2 deletions scripts/downloadSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ async function downloadSchema(): Promise<void> {
'Tags',
'TaggedItems',
'MetricValues',
'MetricValuePartitions'
'MetricValuePartitions',
'Attendances',
'AttendanceOccurrences'
]
// const REST_CONTROLLER_NAMES = (await client.get(
// '/RestControllers/RestControllerNames?includeObsolete=false'
Expand All @@ -39,7 +41,6 @@ async function downloadSchema(): Promise<void> {
definitions: { [path: string]: unknown }
paths: { [path: string]: unknown }
}>(`/doc/v1?controllerName=${name}`)
console.log(name, data)
return { definitions: data.definitions, paths: data.paths }
})
)
Expand Down
14 changes: 14 additions & 0 deletions src/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,17 @@ export const GroupRoleId: DefinedValueMap = {
child: 4,
_default: 3 // Adult
}
export const LocationIdMap = {
Central: 2,
North: 2401,
Unichurch: 2402
}

export const ScheduleIdMap = {
SundayMorning: 4,
SundayNight: 5
}

export const GroupIdMap = {
WeekendService: 28384
}
63 changes: 63 additions & 0 deletions src/extract/checkin/checkin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import z from 'zod'

import { extractFromFluro } from '../lib'

const schema = z.object({
_id: z.string(),
created: z.string().datetime(),
event: z.object({
_id: z.string()
}),
contact: z.object({
_id: z.string()
})
})

export type FluroCheckin = z.infer<typeof schema>

export const extract = extractFromFluro<FluroCheckin>({
contentType: 'checkin',
filterBody: {
allDefinitions: true,
includeArchived: true,
searchInheritable: false,
filter: {
filters: [
{
operator: 'and',
filters: [
{
comparator: '==',
key: '_event.definition',
value: 'service'
},
{
comparator: 'in',
key: '_event.realms',
values: [
{
_id: '5c0d9d3e7ef61e100ae4514b',
title: 'Central'
},
{
_id: '602a45dca496aa1201f4ccc0',
title: 'North'
},
{
_id: '5c0d9d497ef61e100ae45153',
title: 'Unichurch'
}
]
},
{
comparator: '==',
key: 'contact',
value: '5c05059148890574c5395ccb'
}
]
}
]
}
},
schema
})
2 changes: 2 additions & 0 deletions src/extract/checkin/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export type { FluroCheckin } from './checkin'
export { extract } from './checkin'
3 changes: 3 additions & 0 deletions src/extract/client/client.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import axios from 'axios'
import axiosRetry from 'axios-retry'

export const client = axios.create({
baseURL: 'https://api.fluro.io/',
timeout: 60000,
headers: { Authorization: `Bearer ${process.env.FLURO_API_TOKEN}` }
})

axiosRetry(client, { retries: 3 })
44 changes: 30 additions & 14 deletions src/extract/lib/extractFromFluro.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,31 @@
import { get } from 'lodash'
import { type ZodSchema, z } from 'zod'

import { client } from '../client'
import type { ExtractIterator } from '../types'
import { PAGE_SIZE } from '../types'

interface Filter {
comparator?: string
dataType?: 'reference'
key?: string
value?: string
values?: string[] | { _id: string; title: string }[]
}

interface Operator {
operator?: 'and'
filters?: (Operator | Filter)[]
}

interface ExtractFromFluroOptions {
contentType: string
filterBody?: {
allDefinitions?: boolean
includeArchived?: boolean
searchInheritable?: boolean
search?: string
filter?: {
filters?: {
comparator?: string
key?: string
value?: string
values?: string[]
}[]
}
filter?: Operator
}
multipleBody?: {
appendAssignments?: boolean
Expand Down Expand Up @@ -58,12 +65,21 @@ export function extractFromFluro<T>({
return { value: { collection: [], max }, done: true }
} else {
if (schema != null) {
return {
value: {
collection: z.array(schema).parse(req.data) as T[],
max
},
done: false
try {
return {
value: {
collection: z.array(schema).parse(req.data) as T[],
max
},
done: false
}
} catch (e) {
if (e instanceof z.ZodError) {
e.errors.forEach((err) => {
console.log(get(req.data, err.path))
})
}
throw e
}
}
return { value: { collection: req.data, max }, done: false }
Expand Down
2 changes: 2 additions & 0 deletions src/extract/service/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export type { FluroService } from './service'
export { extract } from './service'
75 changes: 75 additions & 0 deletions src/extract/service/service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import z from 'zod'

import { extractFromFluro } from '../lib'

const schema = z
.object({
_id: z.string(),
realms: z
.preprocess(
(val) => {
if (Array.isArray(val)) {
// eslint-disable-next-line
return val.filter((realm) => realm.definition === 'locationRealm')
}
return []
},
z.array(
z.object({
_id: z.string(),
title: z.union([
z.literal('Central'),
z.literal('North'),
z.literal('Unichurch')
]),
definition: z.literal('locationRealm')
})
)
)
.refine((realms) => realms.length === 1)
.transform((realms) => realms[0]),
startDate: z.string().datetime(),
definition: z.literal('service'),
track: z
.object({
_id: z.string(),
title: z.string(),
definition: z.literal('serviceTime')
})
.optional()
})
.transform(({ realms, ...rest }) => ({ realm: realms, ...rest }))

export type FluroService = z.infer<typeof schema>

export const extract = extractFromFluro<FluroService>({
contentType: 'service',
filterBody: {
allDefinitions: true,
includeArchived: true,
searchInheritable: false,
filter: {
filters: [
{
comparator: 'in',
key: 'realms',
values: [
{
_id: '5c0d9d3e7ef61e100ae4514b',
title: 'Central'
},
{
_id: '602a45dca496aa1201f4ccc0',
title: 'North'
},
{
_id: '5c0d9d497ef61e100ae45153',
title: 'Unichurch'
}
]
}
]
}
},
schema
})
4 changes: 3 additions & 1 deletion src/extract/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type { Cache } from '../load/types'

export interface ExtractFn<T> {
(): Promise<AsyncIterator<ExtractIterator<T>>>
(cache: Cache): Promise<AsyncIterator<ExtractIterator<T>>>
}

export interface Realm {
Expand Down
Loading

0 comments on commit ca7f73b

Please sign in to comment.