Skip to content

Commit

Permalink
chore: update baptism step
Browse files Browse the repository at this point in the history
  • Loading branch information
tataihono committed Dec 23, 2024
1 parent 8313f48 commit 3d6be8c
Show file tree
Hide file tree
Showing 9 changed files with 217 additions and 13 deletions.
25 changes: 24 additions & 1 deletion src/extract/attribute/attribute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,6 @@ const attributes: FluroAttribute[] = [
IsRequired: false,
AllowSearch: false
},

{
_id: 'BirthDateVerified',
AbbreviatedName: 'Birth Date Verified',
Expand All @@ -425,6 +424,30 @@ const attributes: FluroAttribute[] = [
IsRequired: false,
AllowSearch: false
},
{
_id: 'BaptismType',
AbbreviatedName: 'Baptism Type',
FieldTypeId: fieldTypes.SingleSelect,
EntityTypeId: entityTypes['Rock.Model.Step'],
Name: 'Baptism Type',
AttributeQualifiers: [
{
IsSystem: false,
Key: 'values',
Value: 'Infant,Adult'
} as components['schemas']['Rock.Model.AttributeQualifier']
],
Description: 'The type of baptism the person underwent',
EntityTypeQualifierColumn: 'StepTypeId',
EntityTypeQualifierValue: '1',
IsSystem: false,
Order: 0,
IsGridColumn: false,
IsMultiValue: false,
IsRequired: false,
AllowSearch: false,
ShowOnBulk: true
},
{
_id: 'Magnification',
AbbreviatedName: 'Magnification Completion Date',
Expand Down
1 change: 1 addition & 0 deletions src/extract/contact/contact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ const schema = z.object({
data: z
.object({
dateofBaptism: z.string().datetime().nullish(),
typeofBaptism: z.enum(['Infant', 'Adult', '']).optional(),
isaChristian: z.enum(['Yes', 'No', 'Unsure', '']).optional()
})
.optional()
Expand Down
53 changes: 53 additions & 0 deletions src/load/contact/baptismStep/attribute/attribute.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { DELETE, POST, RockApiError, type components } from '../../../client'

export async function load(
AttributeValues: {
[key: string]: string | null | undefined
},
data: components['schemas']['Rock.Model.Step']
): Promise<void> {
if (data.Id == null) return

for (const attributeKey of Object.keys(AttributeValues)) {
const attributeValue = AttributeValues[attributeKey]

if (data.AttributeValues?.[attributeKey].Value != attributeValue) {
if (attributeValue == null || attributeValue == '') {
// delete attribute value
const params = {
path: {
id: data.Id
},
query: {
attributeKey
}
}
const { error } = await DELETE('/api/Steps/AttributeValue/{id}', {
params
})
if (error != null)
throw new RockApiError(error, {
cause: { path: params.path, query: params.query }
})
} else {
// update attribute value
const params = {
path: {
id: data.Id
},
query: {
attributeKey,
attributeValue: attributeValue
}
}
const { error } = await POST('/api/Steps/AttributeValue/{id}', {
params
})
if (error != null)
throw new RockApiError(error, {
cause: { path: params.path, query: params.query }
})
}
}
}
}
1 change: 1 addition & 0 deletions src/load/contact/baptismStep/attribute/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { load } from './attribute'
100 changes: 100 additions & 0 deletions src/load/contact/baptismStep/baptismStep.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import { omit } from 'lodash'
import f from 'odata-filter-builder'

import { GET, PATCH, POST, RockApiError, type components } from '../../client'
import type { CacheObject } from '../../types'
import type { RockContact } from '../contact'

import { load as loadAttribute } from './attribute'

export async function load(
person: components['schemas']['Rock.Model.Person'],
value: RockContact
): Promise<void> {
const cacheObject = await loadStep(person, value)

if (cacheObject == null) return

const params = {
path: {
id: cacheObject.rockId
},
query: {
loadAttributes: 'simple' as const
}
}

const { data, error } = await GET('/api/Steps/{id}', {
params
})

if (error != null) throw new RockApiError(error, { cause: { ...params } })

await loadAttribute(value.data.BaptismStep?.AttributeValues ?? {}, data)
}

async function loadStep(
person: components['schemas']['Rock.Model.Person'],
value: RockContact
): Promise<CacheObject | undefined> {
if (person.PrimaryAliasId == null || value.data.BaptismStep == null) return

const params = {
query: {
$filter: f()
.eq('PersonAliasId', person.PrimaryAliasId)
.eq('StepTypeId', 1)
.toString()
}
}

const { data, error } = await GET('/api/Steps', {
params
})

if (error != null)
throw new RockApiError(error, { cause: { query: params.query } })

if (data?.length === 0) {
// create new step
const body = {
PersonAliasId: person.PrimaryAliasId,
StepTypeId: 1,
CampusId: person.PrimaryCampusId,
...omit(value.data.BaptismStep, 'AttributeValues')
}

const { data, error } = await POST('/api/Steps', {
body
})

if (error != null)
throw new RockApiError(error, { cause: { body, id: person.Id } })

return {
rockId: data as unknown as number
}
} else {
// update existing step
const Id = data[0].Id as number

const body = {
PersonAliasId: person.PrimaryAliasId,
StepTypeId: 2,
CampusId: person.PrimaryCampusId,
...omit(value.data.BaptismStep, 'AttributeValues')
}

const { error } = await PATCH('/api/Steps/{id}', {
params: { path: { id: Id } },
body: body as unknown as Record<string, never>
})

if (error != null)
throw new RockApiError(error, { cause: { body, id: person.Id } })

return {
rockId: Id
}
}
}
1 change: 1 addition & 0 deletions src/load/contact/baptismStep/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { load } from './baptismStep'
27 changes: 16 additions & 11 deletions src/load/contact/contact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,34 @@ import type { CacheObject } from '../types'

import { load as loadAttribute } from './attribute'
import { load as loadAvatar } from './avatar'
import { load as loadBaptismStep } from './baptismStep'
import { load as loadNewishStep } from './newishStep'
import { load as loadPersonPreviousName } from './personPreviousName'
import { load as loadNumber } from './phoneNumber'
import { getRecordStatus } from './recordStatus'
import { load as loadTag } from './tag'

type Step =
| {
StepStatusId: number
CompletedDateTime: string | undefined
StartDateTime: string | undefined
EndDateTime: string | undefined
AttributeValues: {
[key: string]: string | null | undefined
}
}
| undefined

export type RockContact = components['schemas']['Rock.Model.Person'] & {
ForeignKey: string
data: {
GroupRoleId: number
PhoneNumber: string[]
FluroRecordStatus: string
PersonPreviousName: string | undefined
NewishStep:
| {
StepStatusId: number
CompletedDateTime: string | undefined
StartDateTime: string | undefined
EndDateTime: string | undefined
AttributeValues: {
[key: string]: string | null | undefined
}
}
| undefined
NewishStep: Step
BaptismStep: Step
AttributeValues: {
[key: string]: string | null | undefined
}
Expand Down Expand Up @@ -59,6 +63,7 @@ export async function load(value: RockContact): Promise<CacheObject> {
await loadAttribute(data, value)
await loadPersonPreviousName(data, value)
await loadNewishStep(data, value)
await loadBaptismStep(data, value)
await loadTag(data, value)

return cacheObject
Expand Down
1 change: 1 addition & 0 deletions src/transform/contact/contact.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ describe('contact', () => {
GroupRoleId: 4,
PersonPreviousName: undefined,
NewishStep: undefined,
BaptismStep: undefined,
TagIds: [1],
AttributeValues: {
FirstVisit: '2021-01-01',
Expand Down
21 changes: 20 additions & 1 deletion src/transform/contact/contact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,25 @@ function transformNewishStep(
}
}

function transformBaptismStep(
data: NonNullable<NonNullable<FluroContact['details']>['faithInfo']>['data']
): RockContact['data']['NewishStep'] {
if (data == null) return undefined

if (data.dateofBaptism != null) {
const date = new Date(data.dateofBaptism).toLocaleDateString('sv')
return {
CompletedDateTime: date,
StartDateTime: date,
EndDateTime: date,
StepStatusId: 2,
AttributeValues: {
BaptismType: data.typeofBaptism == 'Infant' ? 'Infant' : 'Adult'
}
}
}
}

/**
* transforms a fluro api contact object to a rock contact object
*/
Expand Down Expand Up @@ -185,6 +204,7 @@ export function transform(cache: Cache, value: FluroContact): RockContact {
FluroRecordStatus: value.status,
PersonPreviousName: value.maidenName,
NewishStep: transformNewishStep(value.details?.evPathwayDetails?.data),
BaptismStep: transformBaptismStep(value.details?.faithInfo?.data),
TagIds: value.tags
.map((tag) => cache['tag'][tag._id]?.rockId)
.filter(Boolean),
Expand Down Expand Up @@ -223,7 +243,6 @@ export function transform(cache: Cache, value: FluroContact): RockContact {
PoliceVettingRequest:
value.details?.hsTrainingDetails?.data
?.responsetoPoliceVettingRequest,
BaptismDate: value.details?.faithInfo?.data?.dateofBaptism,
core_GivingEnvelopeNumber:
value.details?.financialDetail?.data?.accountNumber?.replace(
/\D/g,
Expand Down

0 comments on commit 3d6be8c

Please sign in to comment.