You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
2.7KB

  1. export type MunicipalityProfile = {
  2. profileId: string
  3. jCode: string
  4. displayName: string | null
  5. updatedAt: string
  6. updatedBy: string
  7. legacyName: string | null
  8. legacyMailingAddress: string | null
  9. legacyCityStateZip: string | null
  10. }
  11. export type MunicipalityProfileValidationError = {
  12. error: string
  13. }
  14. export async function fetchMunicipalityProfiles(
  15. fetcher: typeof fetch = fetch,
  16. ): Promise<MunicipalityProfile[]> {
  17. const response = await fetcher('/api/municipalities/profiles')
  18. if (!response.ok) {
  19. throw new Error(`Failed to load municipality profiles (${response.status})`)
  20. }
  21. return (await response.json()) as MunicipalityProfile[]
  22. }
  23. export async function createMunicipalityProfile(
  24. jCode: string,
  25. displayName: string | null,
  26. fetcher: typeof fetch = fetch,
  27. ): Promise<MunicipalityProfile> {
  28. const response = await fetcher('/api/municipalities/profiles', {
  29. method: 'POST',
  30. headers: { 'Content-Type': 'application/json' },
  31. body: JSON.stringify({ jCode, displayName }),
  32. })
  33. if (response.status === 422) {
  34. const problem = (await response.json()) as MunicipalityProfileValidationError
  35. throw new MunicipalityValidationError(problem.error ?? 'Validation failed.')
  36. }
  37. if (!response.ok) {
  38. throw new Error(`Failed to create municipality profile (${response.status})`)
  39. }
  40. return (await response.json()) as MunicipalityProfile
  41. }
  42. export async function updateMunicipalityProfile(
  43. profileId: string,
  44. displayName: string | null,
  45. fetcher: typeof fetch = fetch,
  46. ): Promise<MunicipalityProfile> {
  47. const response = await fetcher(`/api/municipalities/profiles/${profileId}`, {
  48. method: 'PUT',
  49. headers: { 'Content-Type': 'application/json' },
  50. body: JSON.stringify({ displayName }),
  51. })
  52. if (response.status === 422) {
  53. const problem = (await response.json()) as MunicipalityProfileValidationError
  54. throw new MunicipalityValidationError(problem.error ?? 'Validation failed.')
  55. }
  56. if (!response.ok) {
  57. throw new Error(`Failed to update municipality profile (${response.status})`)
  58. }
  59. return (await response.json()) as MunicipalityProfile
  60. }
  61. export type LegacyJurisdiction = {
  62. jCode: string
  63. name: string | null
  64. }
  65. export async function fetchAvailableJurisdictions(
  66. fetcher: typeof fetch = fetch,
  67. ): Promise<LegacyJurisdiction[]> {
  68. const response = await fetcher('/api/municipalities/jurisdictions')
  69. if (!response.ok) {
  70. throw new Error(`Failed to load jurisdictions (${response.status})`)
  71. }
  72. return (await response.json()) as LegacyJurisdiction[]
  73. }
  74. export class MunicipalityValidationError extends Error {
  75. constructor(message: string) {
  76. super(message)
  77. this.name = 'MunicipalityValidationError'
  78. }
  79. }

Powered by TurnKey Linux.