Skip to content

Commit

Permalink
Refactor io-ts codecs
Browse files Browse the repository at this point in the history
  • Loading branch information
Onitoxan authored and Pl217 committed Sep 3, 2024
1 parent bae58a8 commit e80bb50
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ export default function OrganizationTable(props: OrganizationTableProps) {
data,
}: {
lang: LanguageKey;
data: organizations.SearchOrnganizationResult;
data: organizations.SearchOrganizationResult;
}) => {
const nonSafeTypedTableHeaders = decodeTableHeaders(
query.tableHeaders,
Expand Down Expand Up @@ -290,7 +290,7 @@ export default function OrganizationTable(props: OrganizationTableProps) {
data,
}: {
lang: LanguageKey;
data: organizations.SearchOrnganizationResult;
data: organizations.SearchOrganizationResult;
}) => {
const nonSafeTypedTableHeaders = decodeTableHeaders(
query.tableHeaders,
Expand Down
10 changes: 1 addition & 9 deletions libs/hpc-data/src/lib/categories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,7 @@ export const CATEGORY = t.type({
export type Category = t.TypeOf<typeof CATEGORY>;

export const KEYWORD = t.type({
id: t.number,
name: t.string,
description: t.union([t.string, t.null]),
parentID: t.union([t.number, t.null]),
code: t.union([t.string, t.null]),
group: t.string,
includeTotals: t.union([t.boolean, t.null]),
createdAt: t.string,
updatedAt: t.string,
...CATEGORY.props,
refCount: t.string,
});

Expand Down
2 changes: 1 addition & 1 deletion libs/hpc-data/src/lib/emergencies.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as t from 'io-ts';

const EMERGENCY = t.type({
export const EMERGENCY = t.type({
id: t.number,
name: t.string,
description: t.union([t.string, t.null]),
Expand Down
138 changes: 78 additions & 60 deletions libs/hpc-data/src/lib/flows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const FLOW_OBJECT = t.intersection([

export type FlowObject = t.TypeOf<typeof FLOW_OBJECT>;

const FLOW_CATEGORY = t.intersection([
const FLOW_CATEGORY_REST = t.intersection([
t.type({
name: t.string,
group: t.string,
Expand All @@ -56,7 +56,7 @@ const FLOW_CATEGORY = t.intersection([
}),
}),
]);
export type FlowCategory = t.TypeOf<typeof FLOW_CATEGORY>;
export type FlowCategory = t.TypeOf<typeof FLOW_CATEGORY_REST>;

const CREATED_BY_OR_LAST_UPDATED_BY = t.type({
name: t.string,
Expand Down Expand Up @@ -117,107 +117,125 @@ export const GET_FLOW_RESULT = FLOW_REST;

export type GetFlowResult = t.TypeOf<typeof GET_FLOW_RESULT>;

// GRAPHQL CODE FROM HERE
// * GRAPHQL CODE FROM HERE *

const DIRECTION = t.union([t.literal('source'), t.literal('destination')]);

const FlowLocation = t.type({
const FLOW_LOCATION = t.type({
id: t.number,
name: t.string,
direction: DIRECTION,
});

const FlowOrganization = t.type({
const FLOW_ORGANIZATION = t.type({
id: t.number,
direction: t.union([t.string, t.null, t.undefined]), // Accepts string or null/undefined
name: t.string,
abbreviation: t.string,
});

export type FlowOrganization = t.TypeOf<typeof FlowOrganization>;
const FlowUsageYear = t.type({
export type FlowOrganization = t.TypeOf<typeof FLOW_ORGANIZATION>;
const FLOW_USAGE_YEAR = t.type({
year: t.string,
direction: DIRECTION,
});

const FlowExternalReference = t.type({
const FLOW_EXTERNAL_REFERENCE = t.type({
systemID: t.string,
flowID: t.number,
externalRecordID: t.string,
versionID: t.number,
updatedAt: t.string,
});

const FlowReportDetail = t.type({
id: t.number,
versionID: t.number,
source: t.string,
date: t.union([t.string, t.null]),
verified: t.boolean,
channel: t.union([t.string, t.null]),
updatedAt: t.string,
contactInfo: t.union([t.string, t.null, t.undefined]), // accepts string or null/undefined
sourceID: t.union([t.string, t.null, t.undefined]), // accepts string or null/undefined
refCode: t.union([t.string, t.null, t.undefined]), // accepts string or null/undefined
organizationID: t.union([t.number, t.null, t.undefined]), // accepts number or null/undefined
});
const FLOW_REPORT_DETAIL = t.intersection([
t.type({
id: t.number,
versionID: t.number,
source: t.string,
verified: t.boolean,
updatedAt: t.string,
createdAt: t.string,
}),
t.partial({
flowID: t.number,
date: t.union([t.string, t.null]),
channel: t.union([t.string, t.null]),
contactInfo: t.union([t.string, t.null]),
sourceID: t.union([t.string, t.null]),
refCode: t.union([t.string, t.null]),
organizationID: t.number,
}),
]);

const FlowParkedParentSource = t.type({
const FLOW_PARKED_PARENT_SOURCE = t.type({
organization: t.array(t.number),
orgName: t.array(t.string),
});

const FlowCategoryRef = t.type({
const FLOW_CATEGORY_REF = t.type({
objectID: t.number,
versionID: t.number,
objectType: t.string,
categoryID: t.number,
updatedAt: t.string,
});

const FlowCategory = t.type({
id: t.number,
name: t.string,
group: t.string,
createdAt: t.union([t.string, t.null, t.undefined]), // accepts string or null/undefined
updatedAt: t.union([t.string, t.null, t.undefined]), // accepts string or null/undefined
description: t.union([t.string, t.null, t.undefined]), // accepts string or null/undefined
parentID: t.union([t.number, t.null, t.undefined]), // accepts number or null/undefined
code: t.union([t.string, t.null, t.undefined]), // accepts string or null/undefined
includeTotals: t.union([t.boolean, t.null, t.undefined]), // accepts boolean or null/undefined
categoryRef: FlowCategoryRef,
});
const FLOW_CATEGORY = t.intersection([
t.type({
id: t.number,
name: t.string,
group: t.string,
categoryRef: FLOW_CATEGORY_REF,
}),
t.partial({
createdAt: t.string,
updatedAt: t.string,
description: t.string,
parentID: t.union([t.number, t.null]),
code: t.union([t.string, t.null]),
includeTotals: t.union([t.boolean, t.null]),
}),
]);

const FlowPlan = t.type({
const FLOW_PLAN = t.type({
id: t.number,
name: t.string,
direction: DIRECTION,
});

const FLOW = t.type({
id: t.number,
versionID: t.number,
amountUSD: t.string,
updatedAt: t.string,
activeStatus: t.boolean,
restricted: t.boolean,
locations: t.union([t.array(FlowLocation), t.null, t.undefined]),
categories: t.union([t.array(FlowCategory), t.null, t.undefined]),
organizations: t.union([t.array(FlowOrganization), t.null, t.undefined]),
plans: t.union([t.array(FlowPlan), t.null, t.undefined]),
usageYears: t.union([t.array(FlowUsageYear), t.null, t.undefined]),
childIDs: t.union([t.array(t.number), t.null, t.undefined]),
parentIDs: t.union([t.array(t.number), t.null, t.undefined]), // accepts an array of numbers or null/undefined
origAmount: t.union([t.string, t.null, t.undefined]), // accepts string or null/undefined
origCurrency: t.union([t.string, t.null, t.undefined]), // accepts string or null/undefined
externalReferences: t.array(FlowExternalReference),
reportDetails: t.array(FlowReportDetail),
parkedParentSource: t.union([FlowParkedParentSource, t.null]),
newMoney: t.union([t.boolean, t.null]),
decisionDate: t.union([t.string, t.null]),
flowDate: t.union([t.string, t.null]),
exchangeRate: t.union([t.string, t.null]),
});
export const FLOW = t.intersection([
t.type({
id: t.number,
versionID: t.number,
amountUSD: t.string,
updatedAt: t.string,
activeStatus: t.boolean,
restricted: t.boolean,
externalReferences: t.array(FLOW_EXTERNAL_REFERENCE),
reportDetails: t.array(FLOW_REPORT_DETAIL),
newMoney: t.union([t.boolean, t.null]),
decisionDate: t.union([t.string, t.null]),
flowDate: t.union([t.string, t.null]),
exchangeRate: t.union([t.string, t.null]),
parkedParentSource: t.union([FLOW_PARKED_PARENT_SOURCE, t.null]),
}),
t.partial({
description: t.string,
budgetYear: t.string,
locations: t.union([t.array(FLOW_LOCATION), t.null]),
categories: t.union([t.array(FLOW_CATEGORY), t.null]),
organizations: t.union([t.array(FLOW_ORGANIZATION), t.null]),
destinationOrganizations: t.union([t.array(FLOW_ORGANIZATION), t.null]),
sourceOrganizations: t.union([t.array(FLOW_ORGANIZATION), t.null]),
plans: t.union([t.array(FLOW_PLAN), t.null]),
usageYears: t.union([t.array(FLOW_USAGE_YEAR), t.null]),
childIDs: t.union([t.array(t.number), t.null]),
parentIDs: t.union([t.array(t.number), t.null]),
origAmount: t.union([t.string, t.null]),
origCurrency: t.union([t.string, t.null]),
}),
]);

const FLOW_RESULT = t.array(FLOW);
export type Flow = t.TypeOf<typeof FLOW>;
Expand Down
2 changes: 1 addition & 1 deletion libs/hpc-data/src/lib/global-clusters.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as t from 'io-ts';

const GLOBAL_CLUSTER = t.type({
export const GLOBAL_CLUSTER = t.type({
id: t.number,
hrinfoId: t.union([t.number, t.null]),
type: t.string,
Expand Down
10 changes: 5 additions & 5 deletions libs/hpc-data/src/lib/organizations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,10 @@ const SEARCH_ORGANIZATION = t.type({
create: t.array(UPDATED_CREATED_BY),
update: t.array(UPDATED_CREATED_BY),
});
export type SearchOrganiation = t.TypeOf<typeof SEARCH_ORGANIZATION>;
export type SearchOrganization = t.TypeOf<typeof SEARCH_ORGANIZATION>;

export const SEARCH_ORGANIZATIONS = t.array(SEARCH_ORGANIZATION);
export type SearchOrganiations = t.TypeOf<typeof SEARCH_ORGANIZATIONS>;
export type SearchOrganizations = t.TypeOf<typeof SEARCH_ORGANIZATIONS>;

export const GET_ORGANIZATIONS_AUTOCOMPLETE_PARAMS = t.type({
query: t.string,
Expand Down Expand Up @@ -136,7 +136,7 @@ export const SEARCH_ORGANIZATION_RESULT = t.type({
organizations: SEARCH_ORGANIZATIONS,
});

export type SearchOrnganizationResult = t.TypeOf<
export type SearchOrganizationResult = t.TypeOf<
typeof SEARCH_ORGANIZATION_RESULT
>;

Expand All @@ -161,7 +161,7 @@ export const CREATE_ORGANIZATION_PARAMS = t.type({
url: t.string,
notes: t.string,
comments: t.string,
verfied: t.boolean,
verified: t.boolean,
parentID: t.number,
}),
]),
Expand Down Expand Up @@ -252,7 +252,7 @@ export interface Model {
): Promise<GetOrganizationsResult>;
searchOrganizations(
params: SearchOrganizationParams
): Promise<SearchOrnganizationResult>;
): Promise<SearchOrganizationResult>;
getOrganization(
params: GetOrganizationParams
): Promise<GetOrganizationResult>;
Expand Down
2 changes: 1 addition & 1 deletion libs/hpc-data/src/lib/projects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const PDF = t.type({
]),
});

const PROJECT = t.type({
export const PROJECT = t.type({
id: t.number,
createdAt: t.string,
updatedAt: t.string,
Expand Down
2 changes: 1 addition & 1 deletion libs/hpc-dummy/src/lib/dummy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1127,7 +1127,7 @@ export class Dummy {
),
searchOrganizations: dummyEndpoint(
'organizations.searchOrganizations',
async (): Promise<organizations.SearchOrnganizationResult> => {
async (): Promise<organizations.SearchOrganizationResult> => {
return {
count: '1',
organizations: [
Expand Down

0 comments on commit e80bb50

Please sign in to comment.