-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: split data transform logic out of model and upgrade sheethu…
…ahua to v3
- Loading branch information
Showing
39 changed files
with
632 additions
and
667 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
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
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,69 @@ | ||
import { | ||
AssemblyName, | ||
AssemblyPartyGroup, | ||
assemblyStaticInfoMap, | ||
type Assembly | ||
} from '$models/assembly'; | ||
import type { Party } from '$models/party'; | ||
import { safeFind } from '../processor'; | ||
import { fetchParties } from './party'; | ||
import { sheets, withCache } from './shared'; | ||
import { | ||
asDate, | ||
asNumber, | ||
asOneOf, | ||
asString, | ||
Column, | ||
createTransformer, | ||
Object, | ||
type StaticDecode | ||
} from 'sheethuahua'; | ||
|
||
export const fetchAssemblies = withCache('Assemblies', async (): Promise<Assembly[]> => { | ||
const parties = await fetchAssemblyPartyGroups(); | ||
const groups = await fetchParties(); | ||
|
||
const assemblySchema = Object({ | ||
id: Column('id', asString()), | ||
name: Column('name', asOneOf(global.Object.values(AssemblyName))), | ||
term: Column('term', asNumber()), | ||
startedAt: Column('startedAt', asDate()), | ||
endedAt: Column('endedAt', asDate().optional()), | ||
origin: Column('origin', asString().optional()), | ||
governmentParties: Column('id', asPartyGroup(AssemblyPartyGroup.Government, parties, groups)), | ||
oppositionParties: Column('id', asPartyGroup(AssemblyPartyGroup.Opposition, parties, groups)), | ||
mainRoles: Column('name', asMainRoles()), | ||
abbreviation: Column('name', asAbbreviation()) | ||
}); | ||
|
||
return sheets.get('Assemblies', assemblySchema); | ||
}); | ||
|
||
export const fetchAssemblyPartyGroups = withCache('AssemblyPartyGroups', () => | ||
sheets.get('AssemblyPartyGroups', assemblyPartyGroupSchema) | ||
); | ||
|
||
const assemblyPartyGroupSchema = Object({ | ||
assemblyId: Column('assemblyId', asString()), | ||
partyName: Column('partyName', asString()), | ||
group: Column('group', asOneOf(global.Object.values(AssemblyPartyGroup))) | ||
}); | ||
|
||
const asPartyGroup = ( | ||
groupName: AssemblyPartyGroup, | ||
groups: StaticDecode<typeof assemblyPartyGroupSchema>[], | ||
parties: Party[] | ||
) => | ||
createTransformer((id: string) => | ||
groups | ||
.filter(({ assemblyId, group }) => assemblyId === id && group === groupName) | ||
.map(({ partyName }) => safeFind(parties, ({ name }) => name === partyName)) | ||
); | ||
|
||
const asMainRoles = () => | ||
createTransformer((name: string) => assemblyStaticInfoMap[name as AssemblyName]?.mainRoles ?? []); | ||
|
||
const asAbbreviation = () => | ||
createTransformer( | ||
(name: string) => assemblyStaticInfoMap[name as AssemblyName]?.abbreviation ?? '' | ||
); |
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,31 @@ | ||
import { | ||
BillEventActionType, | ||
BillEventType, | ||
eventTypeTitleDescription, | ||
type BillEvent | ||
} from '$models/bill-event'; | ||
import { asSlug } from '../transformers'; | ||
import { sheets, withCache } from './shared'; | ||
import { asDate, asOneOf, asString, Column, Object } from 'sheethuahua'; | ||
|
||
const billEventSchema = Object({ | ||
billId: Column('billId', asSlug()), | ||
date: Column('date', asDate().optional()), | ||
type: Column('type', asOneOf(global.Object.values(BillEventType))), | ||
title: Column('title', asString().optional()), | ||
description: Column('description', asString().optional()), | ||
actionType: Column('actionType', asOneOf(global.Object.values(BillEventActionType)).optional()), | ||
votedInVotingId: Column('votedInVotingId', asSlug().optional()), | ||
mergedIntoBillId: Column('mergedIntoBillId', asSlug().optional()), | ||
enforcementDocumentUrl: Column('enforcementDocumentUrl', asString().optional()) | ||
}); | ||
|
||
export const fetchBillEvents = withCache( | ||
'BillEvents', | ||
async (): Promise<BillEvent[]> => | ||
(await sheets.get('BillEvents', billEventSchema)).map((e) => ({ | ||
...e, | ||
title: e.title ?? eventTypeTitleDescription[e.type].title, | ||
description: e.description ?? eventTypeTitleDescription[e.type].description | ||
})) | ||
); |
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,45 @@ | ||
import { BillProposerType, BillStatus, type Bill, type PeopleProposer } from '$models/bill'; | ||
import type { Link } from '$models/link'; | ||
import { asPolitician, asSlug, asValidAssembly } from '../transformers'; | ||
import { sheets, withCache } from './shared'; | ||
import { asDate, asOneOf, asString, Column, Object, asArray, asNumber } from 'sheethuahua'; | ||
|
||
export const fetchBills = withCache('Bills', async (): Promise<Bill[]> => { | ||
const billSchema = Object({ | ||
id: Column('id', asSlug()), | ||
acceptanceNumber: Column('acceptanceNumber', asString()), | ||
title: Column('title', asString()), | ||
nickname: Column('nickname', asString().optional()), | ||
description: Column('description', asString().optional()), | ||
status: Column('status', asOneOf(global.Object.values(BillStatus))), | ||
categories: Column('categories', asArray(asString()).optional([])), | ||
proposedOn: Column('proposedOn', asDate()), | ||
attachment: Object({ | ||
name: Column('attachmentName', asString().optional()), | ||
url: Column('attachmentUrl', asString().optional()) | ||
}), | ||
proposedLedByPolitician: Column('proposedLedByPoliticianId', await asPolitician()).optional(), | ||
proposedByAssembly: Column('proposedByAssemblyId', await asValidAssembly()).optional(), | ||
proposedByPeople: Object({ | ||
ledBy: Column('proposedLedByPeople', asString().optional()), | ||
signatoryCount: Column('peopleSignatureCount', asNumber().optional(0)) | ||
}), | ||
lisUrl: Column('lisUrl', asString()) | ||
}); | ||
|
||
return (await sheets.get('Bills', billSchema)).map((bill) => ({ | ||
...bill, | ||
nickname: bill.nickname ?? bill.title.replace('ร่างพระราชบัญญัติ', 'ร่าง พ.ร.บ.'), | ||
proposerType: bill.proposedLedByPolitician | ||
? BillProposerType.Politician | ||
: bill.proposedByAssembly | ||
? BillProposerType.Assembly | ||
: bill.proposedByPeople | ||
? BillProposerType.People | ||
: BillProposerType.Unknown, | ||
proposedByPeople: bill.proposedByPeople.ledBy | ||
? (bill.proposedByPeople as PeopleProposer) | ||
: undefined, | ||
attachment: bill.attachment.name && bill.attachment.url ? (bill.attachment as Link) : undefined | ||
})); | ||
}); |
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,16 @@ | ||
import type { Party } from '$models/party'; | ||
import { asStaticImage } from '../transformers'; | ||
import { sheets, withCache } from './shared'; | ||
import { asString, Column, Object } from 'sheethuahua'; | ||
|
||
const DEFAULT_PARTY_COLOR = '#F4F4F4'; | ||
|
||
export const fetchParties = withCache('Parties', (): Promise<Party[]> => { | ||
const partySchema = Object({ | ||
name: Column('name', asString()), | ||
color: Column('color', asString().optional(DEFAULT_PARTY_COLOR)), | ||
logo: Column('name', asStaticImage('/images/parties')) | ||
}); | ||
|
||
return sheets.get('Parties', partySchema); | ||
}); |
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,87 @@ | ||
import type { Link } from '$models/link'; | ||
import type { AssemblyRoleHistory, PartyRoleHistory, Politician } from '$models/politician'; | ||
import { joinMany } from '../processor'; | ||
import { asValidAssembly, asMarkdownList, asValidParty, asStaticImage } from '../transformers'; | ||
import { sheets, withCache } from './shared'; | ||
import { asDate, asNumber, asString, Column, createTransformer, Object, Tuple } from 'sheethuahua'; | ||
|
||
export const fetchPoliticians = withCache('Politicians', async (): Promise<Politician[]> => { | ||
const politicianSchema = Object({ | ||
id: Column('id', asString()), | ||
prefix: Column('prefix', asString().optional()), | ||
firstname: Column('firstname', asString()), | ||
lastname: Column('lastname', asString()), | ||
avatar: Column('id', asStaticImage('/images/politicians')), | ||
sex: Column('sex', asString().optional()), | ||
birthdate: Column('birthdate', asDate().optional()), | ||
educations: Column('educations', asMarkdownList().optional([])), | ||
previousOccupations: Column('previousOccupations', asMarkdownList().optional([])), | ||
contacts: Tuple([ | ||
Column('facebook', asSocialContact('Facebook').optional()), | ||
Column('x', asSocialContact('X').optional()) | ||
]) | ||
}); | ||
|
||
const partyRoleHistory = await fetchPartyRoleHistory(); | ||
const assemblyRoleHistory = await fetchAssemblyRoleHistory(); | ||
|
||
return (await sheets.get('Politicians', politicianSchema)).map((politician) => { | ||
const partyRoles = joinMany(partyRoleHistory, 'politicianId', politician.id).sort( | ||
(a, b) => b.startedAt.getTime() - a.startedAt.getTime() | ||
); | ||
const assemblyRoles = joinMany(assemblyRoleHistory, 'politicianId', politician.id).sort( | ||
(a, b) => b.startedAt.getTime() - a.startedAt.getTime() | ||
); | ||
|
||
return { | ||
...politician, | ||
contacts: politician.contacts.filter((c) => c) as Link[], | ||
partyRoles, | ||
assemblyRoles, | ||
isActive: | ||
assemblyRoles.length > 0 && !assemblyRoles[0].endedAt && !assemblyRoles[0].assembly.endedAt | ||
}; | ||
}); | ||
}); | ||
|
||
export const fetchAssemblyRoleHistory = withCache( | ||
'AssemblyRoleHistory', | ||
async (): Promise<AssemblyRoleHistory[]> => { | ||
const assemblyRoleSchema = Object({ | ||
politicianId: Column('politicianId', asString()), | ||
assembly: Column('assemblyId', await asValidAssembly()), | ||
role: Column('role', asString().optional('สมาชิก')), | ||
appointmentMethod: Column('appointmentMethod', asString().optional()), | ||
province: Column('province', asString().optional()), | ||
districtNumber: Column('districtNumber', asNumber().optional()), | ||
listNumber: Column('listNumber', asNumber().optional()), | ||
startedAt: Column('startedAt', asDate()), | ||
endedAt: Column('endedAt', asDate().optional()) | ||
}); | ||
|
||
return sheets.get('AssemblyRoleHistory', assemblyRoleSchema); | ||
} | ||
); | ||
|
||
export const fetchPartyRoleHistory = withCache( | ||
'PartyRoleHistory', | ||
async (): Promise<PartyRoleHistory[]> => { | ||
const partyRoleSchema = Object({ | ||
politicianId: Column('politicianId', asString()), | ||
party: Column('partyName', await asValidParty()), | ||
role: Column('role', asString().optional('สมาชิก')), | ||
startedAt: Column('startedAt', asDate()), | ||
endedAt: Column('endedAt', asDate().optional()) | ||
}); | ||
|
||
return sheets.get('PartyRoleHistory', partyRoleSchema); | ||
} | ||
); | ||
|
||
const asSocialContact = (label: string) => | ||
createTransformer( | ||
(url): Link => ({ | ||
label, | ||
url | ||
}) | ||
); |
Oops, something went wrong.