Skip to content

Commit

Permalink
Merge pull request #458 from Open-Earth-Foundation/fix/region-country…
Browse files Browse the repository at this point in the history
…-locode

feat(api): add countryLocode and regionLocode to city
  • Loading branch information
lemilonkh committed Apr 24, 2024
2 parents 346ef1a + c618060 commit f5e9a6e
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 16 deletions.
35 changes: 35 additions & 0 deletions app/migrations/20240424131954-add-country-region-locode.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"use strict";

/** @type {import('sequelize-cli').Migration} */
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.sequelize.transaction(async (transaction) => {
await queryInterface.addColumn(
"City",
"country_locode",
Sequelize.STRING,
{ transaction },
);

await queryInterface.addColumn(
"City",
"region_locode",
Sequelize.STRING,
{
transaction,
},
);
});
},

async down(queryInterface) {
await queryInterface.sequelize.transaction(async (transaction) => {
await queryInterface.removeColumn("City", "country_locode", {
transaction,
});
await queryInterface.removeColumn("City", "region_locode", {
transaction,
});
});
},
};
32 changes: 18 additions & 14 deletions app/src/app/[lng]/onboarding/setup/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -717,31 +717,35 @@ export default function OnboardingSetup({
const regionPopulationYear = watch("regionPopulationYear");
const countryPopulationYear = watch("countryPopulationYear");

const { data: cityArea, isLoading: isCityAreaLoading } = api.useGetCityBoundaryQuery(data.locode!, {
skip: !data.locode,
});
const { data: cityArea, isLoading: isCityAreaLoading } =
api.useGetCityBoundaryQuery(data.locode!, {
skip: !data.locode,
});

const onConfirm = async () => {
// save data in backend
setConfirming(true);
let city: CityAttributes | null = null;

let area = ocCityData?.area ?? 0;
let region =
ocCityData?.root_path_geo.filter((item: any) => item.type === "adm1")[0]
?.name ?? "";
let country =
ocCityData?.root_path_geo.filter(
(item: any) => item.type === "country",
)[0]?.name ?? "";
const area = cityArea?.area ?? ocCityData?.area ?? undefined;
const region = ocCityData?.root_path_geo.filter(
(item: any) => item.type === "adm1",
)[0];
const regionName = region?.name ?? "";
const country = ocCityData?.root_path_geo.filter(
(item: any) => item.type === "country",
)[0];
const countryName = country?.name ?? "";

try {
city = await addCity({
name: data.name,
locode: data.locode!,
area: Math.round(cityArea?.area!),
region,
country,
area,
region: regionName,
country: countryName,
regionLocode: region?.actor_id ?? undefined,
countryLocode: country?.actor_id ?? undefined,
}).unwrap();
await addCityPopulation({
cityId: city.cityId,
Expand Down
16 changes: 16 additions & 0 deletions app/src/models/City.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ export interface CityAttributes {
shape?: object;
country?: string;
region?: string;
countryLocode?: string;
regionLocode?: string;
area?: number;
created?: Date;
lastUpdated?: Date;
Expand All @@ -25,6 +27,8 @@ export type CityOptionalAttributes =
| "shape"
| "country"
| "region"
| "countryLocode"
| "regionLocode"
| "area"
| "created"
| "lastUpdated";
Expand All @@ -43,6 +47,8 @@ export class City
shape?: object;
country?: string;
region?: string;
countryLocode?: string;
regionLocode?: string;
area?: number;
created?: Date;
lastUpdated?: Date;
Expand Down Expand Up @@ -162,6 +168,16 @@ export class City
type: DataTypes.STRING(255),
allowNull: true,
},
countryLocode: {
type: DataTypes.STRING(255),
allowNull: true,
field: "country_locode",
},
regionLocode: {
type: DataTypes.STRING(255),
allowNull: true,
field: "region_locode",
},
area: {
type: DataTypes.BIGINT,
allowNull: true,
Expand Down
6 changes: 4 additions & 2 deletions app/src/services/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export const api = createApi({
transformResponse: (response: {
data: GeoJSON;
boundingBox: BoundingBox;
area: number
area: number;
}) => response,
}),
getInventory: builder.query<InventoryResponse, string>({
Expand All @@ -68,9 +68,11 @@ export const api = createApi({
{
name: string;
locode: string;
area: number;
area: number | undefined;
region: string;
country: string;
regionLocode: string;
countryLocode: string;
}
>({
query: (data) => ({
Expand Down
2 changes: 2 additions & 0 deletions app/src/util/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ export const createCityRequest = z.object({
shape: geoJSON.optional(),
country: z.string().optional(),
region: z.string().optional(),
countryLocode: z.string().optional(),
regionLocode: z.string().optional(),
area: z.number().int().optional(),
});
export type CreateCityRequest = z.infer<typeof createCityRequest>;
Expand Down

0 comments on commit f5e9a6e

Please sign in to comment.