diff --git a/util/query.ts b/util/query.ts index 6d7640d4..bee93c11 100644 --- a/util/query.ts +++ b/util/query.ts @@ -1,97 +1,89 @@ -const where = { +import { Query, CreateQueryParams } from "./types"; + +const WHERE = { confirmed: `(Confirmed > 0)`, deaths: `(Deaths > 0)`, recovered: `(Recovered <> 0)`, all: `1=1` }; -export const createQuery = ({ where }) => ({ +export const createQuery = (queryParams: CreateQueryParams): Query => ({ f: "json", outFields: "*", returnGeometry: false, - where + ...queryParams, + ...(queryParams.field != undefined && { + outStatistics: `[{"statisticType":"sum","onStatisticField":"${queryParams.field}","outStatisticFieldName":"value"}]` + }) }); -export const withCountryRegion = (where: string, countryRegion?: string) => +export const withCountryRegion = (where: string, countryRegion?: string): string => countryRegion ? `${where} AND (Country_Region='${countryRegion}')` : where; -export const createArrayQuery = ({ where, orderByFields }) => ({ - ...createQuery({ where }), - orderByFields -}); - export const queryConfirmed = ( countryRegion?: string, shouldUseProvinceState?: boolean -) => - createArrayQuery({ +): Query => + createQuery({ where: countryRegion - ? withCountryRegion(where.confirmed, countryRegion) - : where.confirmed, - orderByFields: `Confirmed desc, Country_Region asc${ - shouldUseProvinceState ? ",Province_State asc" : "" - }` + ? withCountryRegion(WHERE.confirmed, countryRegion) + : WHERE.confirmed, + orderByFields: `Confirmed desc, Country_Region asc${shouldUseProvinceState ? ",Province_State asc" : "" + }` }); export const queryDeaths = ( countryRegion?: string, shouldUseProvinceState?: boolean -) => - createArrayQuery({ +): Query => + createQuery({ where: countryRegion - ? withCountryRegion(where.deaths, countryRegion) - : where.deaths, - orderByFields: `Deaths desc, Country_Region asc${ - shouldUseProvinceState ? ",Province_State asc" : "" - }` + ? withCountryRegion(WHERE.deaths, countryRegion) + : WHERE.deaths, + orderByFields: `Deaths desc, Country_Region asc${shouldUseProvinceState ? ",Province_State asc" : "" + }` }); export const queryRecovered = ( countryRegion?: string, shouldUseProvinceState?: boolean -) => - createArrayQuery({ +): Query => + createQuery({ where: countryRegion - ? withCountryRegion(where.recovered, countryRegion) - : where.recovered, - orderByFields: `Recovered desc, Country_Region asc${ - shouldUseProvinceState ? ",Province_State asc" : "" - }` + ? withCountryRegion(WHERE.recovered, countryRegion) + : WHERE.recovered, + orderByFields: `Recovered desc, Country_Region asc${shouldUseProvinceState ? ",Province_State asc" : "" + }` }); export const queryLastUpdate = (countryRegion?: string) => ({ - ...createArrayQuery({ - where: withCountryRegion(where.confirmed, countryRegion), + ...createQuery({ + where: withCountryRegion(WHERE.confirmed, countryRegion), orderByFields: "Last_Update desc" }), resultRecordCount: 1 }); -export const createTotalQuery = ({ where, field }) => ({ - ...createQuery({ where }), - outStatistics: `[{"statisticType":"sum","onStatisticField":"${field}","outStatisticFieldName":"value"}]` -}); - -export const queryTotalConfirmed = (countryRegion?: string) => - createTotalQuery({ - where: withCountryRegion(where.confirmed, countryRegion), +export const queryTotalConfirmed = (countryRegion?: string): Query => + createQuery({ + where: withCountryRegion(WHERE.confirmed, countryRegion), field: "Confirmed" }); -export const queryTotalDeaths = (countryRegion?: string) => - createTotalQuery({ - where: withCountryRegion(where.deaths, countryRegion), +export const queryTotalDeaths = (countryRegion?: string): Query => + createQuery({ + where: withCountryRegion(WHERE.deaths, countryRegion), field: "Deaths" }); -export const queryTotalRecovered = (countryRegion?: string) => - createTotalQuery({ - where: withCountryRegion(where.recovered, countryRegion), - field: "Recovered" +export const queryTotalRecovered = (countryRegion?: string): Query => + createQuery({ + where: withCountryRegion(WHERE.recovered, countryRegion), + field: "Recovered", }); -export const queryCasesTimeSeries = () => - createArrayQuery({ - where: where.all, +export const queryCasesTimeSeries = (): Query => + createQuery({ + where: WHERE.all, orderByFields: "Report_Date_String asc" }); diff --git a/util/types.ts b/util/types.ts new file mode 100644 index 00000000..4484d4f3 --- /dev/null +++ b/util/types.ts @@ -0,0 +1,14 @@ +export interface Query { + f: string + outFields: string + returnGeometry: boolean + where: string + orderByFields?: string + outStatistics?: string +} + +export interface CreateQueryParams { + where: string, + orderByFields?: string + field?: string +} \ No newline at end of file