Skip to content

Commit

Permalink
Merge pull request #14 from OpenUpSA/dev/change-to-using-field-ids-fo…
Browse files Browse the repository at this point in the history
…r-rest

Changed all Airtable field names to Ids
  • Loading branch information
paulmwatson authored Jul 23, 2024
2 parents a26e47b + 56f213d commit ff9f342
Show file tree
Hide file tree
Showing 3 changed files with 201 additions and 86 deletions.
82 changes: 82 additions & 0 deletions airtableFieldMappings.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Column (field) names in Airtable can be changed by users which would break code
// so use field IDs for retrieval and map them to names that do not change in this
// code.
// Get IDs here:
// https://airtable.com/appZdj1pFZQOBMn4E/api/docs#curl/table:power%20stations:fields

export const PowerStationFieldIdToNameMapping = {
fldc5VoKdeCbXtKCX: "Name",
fld9sbxP5gHs8qcsO: "FuelType",
fldZ2HLhUsFTg7XSn: "Region",
fldaZuTUqYb2YEU15: "Latitude",
fldxBXN0c81g4uGK1: "Longitude",
fldPmhaZxEMTUcDWs: "Operator",
fldghd96iP7HOINzp: "Owner",
fldmZ00VzZXiMXThI: "Output",
fldqby1mB9xtP4QuH: "CommissionStart",
fldPx6NI6hAQk8BS1: "CommissionEnd",
flduS2NRXXaClldbM: "DecommissionStart",
flddtGBZFMGPR1Vgq: "DecommissionEnd",
flddOvnQtlljYZpVw: "ThumbnailImage",
fldvI25Q57sQFS9RJ: "ShortDescription",
};

// And inverse as helper
export const PowerStationFieldNameToIdMapping = Object.fromEntries(
Object.entries(PowerStationFieldIdToNameMapping).map(([id, name]) => [
name,
id,
])
);

export const RegionFieldIdToNameMapping = {
fldUjlD7kYOge8fS0: "Name",
};

export const RegionFieldNameToIdMapping = Object.fromEntries(
Object.entries(RegionFieldIdToNameMapping).map(([id, name]) => [name, id])
);

export const FuelTypeFieldIdToNameMapping = {
fldnEX77eOrhwz0B0: "Name",
fldjHxR8L2XbSXlqB: "Shorthand",
fldX24kZhRaaQkVCD: "RGBColor",
fldA8ww1crWDDstuk: "Icon",
};

export const FuelTypeFieldNameToIdMapping = Object.fromEntries(
Object.entries(FuelTypeFieldIdToNameMapping).map(([id, name]) => [name, id])
);

export const EntityFieldIdToNameMapping = {
fldT2KxRmj72cucaA: "Name",
fld6QU0bo6XWODmTs: "Role",
fldbZyLm3ZBGL7RxJ: "Controversies",
fldkQc4JxSfJjIDrW: "EntityType",
fldlGae0cm0lXqrYw: "Country",
fldoXxxYNmcUUlRrw: "Image",
fld8ZhBjQ9KQf9sqg: "Details",
fldFyUdwCXbJFyMBO: "Established",
};

export const EntityFieldNameToIdMapping = Object.fromEntries(
Object.entries(EntityFieldIdToNameMapping).map(([id, name]) => [name, id])
);

export const CountryFieldIdToNameMapping = {
fldWkbfmIfSlt3MmF: "Name",
};

export const CountryFieldNameToIdMapping = Object.fromEntries(
Object.entries(CountryFieldIdToNameMapping).map(([id, name]) => [name, id])
);

export const EntityRoleFieldIdToNameMapping = {
fldgQBH9vWJeVXxl1: "Role",
fldV2u9ZlbK1Z8mnP: "Entity",
fld72lL4RmFWS7NU1: "PersonPolitician",
};

export const EntityRoleFieldNameToIdMapping = Object.fromEntries(
Object.entries(EntityRoleFieldIdToNameMapping).map(([id, name]) => [name, id])
);
72 changes: 45 additions & 27 deletions app/api/entity-info/route.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import { Entity, EntityRole, Country } from "@/types";
import {
EntityFieldIdToNameMapping,
EntityFieldNameToIdMapping,
CountryFieldNameToIdMapping,
CountryFieldIdToNameMapping,
EntityRoleFieldNameToIdMapping,
EntityRoleFieldIdToNameMapping,
} from "@/airtableFieldMappings";

import Airtable from "airtable";
import { NextResponse, NextRequest } from "next/server";
Expand All @@ -8,30 +16,24 @@ export async function GET(request: NextRequest) {

const base = Airtable.base("appZdj1pFZQOBMn4E");

const countriesTable = base("Country (Ref)").select({
const countriesTable = base("tblaY5bXfxdoY7Bxa").select({
view: "Grid view",
fields: ["Name"],
fields: Object.keys(CountryFieldIdToNameMapping),
returnFieldsByFieldId: true,
});
const countries: Country[] = [];

const entitiesTable = base("Entities").select({
const entitiesTable = base("tbliu88eOfPtmIMRO").select({
view: "All Entities",
fields: [
"Name",
"Role",
"Controversies",
"Entity Type",
"Country",
"Image",
"Details",
"Established",
],
fields: Object.keys(EntityFieldIdToNameMapping),
returnFieldsByFieldId: true,
});
const entities: Entity[] = [];

const entityRolesTable = base("Entity Roles (Junction)").select({
const entityRolesTable = base("tblygJBhM0TzGgit6").select({
view: "Live",
fields: ["Role", "Entity", "Person / Politician"],
fields: Object.keys(EntityRoleFieldIdToNameMapping),
returnFieldsByFieldId: true,
});
const entityRoles: EntityRole[] = [];

Expand All @@ -40,7 +42,7 @@ export async function GET(request: NextRequest) {
records.forEach(({ fields, id }) => {
countries.push({
id: id as Country["id"],
name: fields.Name as Country["name"],
name: fields[CountryFieldNameToIdMapping["Name"]] as Country["name"],
});
});
processNextPage();
Expand All @@ -49,10 +51,14 @@ export async function GET(request: NextRequest) {
records.forEach(({ fields, id }) => {
entityRoles.push({
id: id as EntityRole["id"],
role: fields.Role as EntityRole["role"],
entity_id: fields.Entity as EntityRole["entity_id"],
role: fields[
EntityRoleFieldNameToIdMapping["Role"]
] as EntityRole["role"],
entity_id: fields[
EntityRoleFieldNameToIdMapping["Entity"]
] as EntityRole["entity_id"],
personPolitician_id: fields[
"Person / Politician"
EntityRoleFieldNameToIdMapping["PersonPolitician"]
] as EntityRole["personPolitician_id"],
});
});
Expand All @@ -62,14 +68,26 @@ export async function GET(request: NextRequest) {
records.forEach(({ fields, id }) => {
entities.push({
id: id as Entity["id"],
name: fields.Name as Entity["name"],
role_id: fields.Role as Entity["role_id"],
controversies: fields.Controversies as Entity["controversies"],
entityType: fields["Entity Type"] as Entity["entityType"],
country_id: fields.Country as Entity["country_id"],
image: fields.Image as Entity["image"],
details: fields.Details as Entity["details"],
established: fields.Established as Entity["established"],
name: fields[EntityFieldNameToIdMapping["Name"]] as Entity["name"],
role_id: fields[
EntityFieldNameToIdMapping["Role"]
] as Entity["role_id"],
controversies: fields[
EntityFieldNameToIdMapping["Controversies"]
] as Entity["controversies"],
entityType: fields[
EntityFieldNameToIdMapping["EntityType"]
] as Entity["entityType"],
country_id: fields[
EntityFieldNameToIdMapping["Country"]
] as Entity["country_id"],
image: fields[EntityFieldNameToIdMapping["Image"]] as Entity["image"],
details: fields[
EntityFieldNameToIdMapping["Details"]
] as Entity["details"],
established: fields[
EntityFieldNameToIdMapping["Established"]
] as Entity["established"],
});
});
processNextPage();
Expand Down
Loading

0 comments on commit ff9f342

Please sign in to comment.