-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
217 additions
and
13 deletions.
There are no files selected for viewing
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,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 } | ||
}) | ||
} | ||
} | ||
} | ||
} |
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 @@ | ||
export { load } from './attribute' |
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,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 | ||
} | ||
} | ||
} |
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 @@ | ||
export { load } from './baptismStep' |
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