Skip to content

Commit

Permalink
Damon/ch362 add sdg numbers to db (#70)
Browse files Browse the repository at this point in the history
* Add SDG numbers to DB, connect to UI

* Sort Interests by name, InterestTypes by sort order

* Fix types

* Build on prepush to make sure we don't break the build
  • Loading branch information
orbiteleven authored Mar 7, 2021
1 parent a262fac commit a6804d5
Show file tree
Hide file tree
Showing 10 changed files with 381 additions and 277 deletions.
5 changes: 3 additions & 2 deletions graphql/queries/query-all-interests-by-type.graphql
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
query queryAllInterestsByType {
interestTypes {
interestTypes(orderBy: { sortOrder: asc }) {
id
name
parentInterestTypeId
Interests {
Interests(orderBy: { name: asc }) {
id
name
sdgNumber
}
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"start": "node server.mjs",
"lint": "eslint . --ext .js,.mjs,.ts,.tsx",
"test": "rm -rf .next out && jest",
"test:prepush": "yarn lint --quiet && yarn cover",
"test:prepush": "yarn lint --quiet && yarn cover && yarn build",
"test:ci": "rm -rf .next out && jest",
"cover": "rm -rf .next && jest --coverage",
"postinstall": "yarn generate",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-- AlterTable
ALTER TABLE "interests" ADD COLUMN "description" TEXT,
ADD COLUMN "sdg_number" TEXT;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "interest_types" ADD COLUMN "sort_order" INTEGER NOT NULL DEFAULT 0;
9 changes: 7 additions & 2 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,11 @@ model Activity {
}

model Interest {
id String @id @default(uuid())
name String
id String @id @default(uuid())
name String
description String?
sdgNumber String? @map("sdg_number")
InterestType InterestType @relation(fields: [interestTypeId], references: [id])
interestTypeId String @map("interest_type_id")
Expand All @@ -169,11 +172,13 @@ model InterestType {
name String
parent InterestType? @relation("parent_interest_type", fields: [parentInterestTypeId], references: [id])
children InterestType[] @relation("parent_interest_type")
sortOrder Int @default(0) @map("sort_order")
parentInterestTypeId String? @map("parent_interest_type_id")
Interests Interest[]
@@unique(name: "nameUniqueForParentInterestType", fields: [parentInterestTypeId, name])
@@index([sortOrder])
@@map("interest_types")
}

Expand Down
186 changes: 102 additions & 84 deletions prisma/seeds/interests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,132 +2,150 @@ import { PrismaClient } from '@prisma/client'

import { Interest, InterestType } from '@generated/type-graphql'

interface interestMap {
name: string
description: string
sdgNumber: string
}
interface interestTypeMap {
id?: string
name: string
sortOrder?: number
types?: interestTypeMap[]
interests?: string[]
interests?: interestMap[]
}

const interestTypes: interestTypeMap[] = [
{
name: 'Focus',
sortOrder: 1,
types: [
{
name: 'Environment',
sortOrder: 1,
interests: [
'Air',
'Biodiversity land',
'Biodiversity water',
'Clean freshwater',
'Clean oceans',
'Climate change',
'Forest',
'Soil',
{ name: 'Air', description: '', sdgNumber: '' },
{ name: 'Biodiversity land', description: '', sdgNumber: '15' },
{ name: 'Biodiversity water', description: '', sdgNumber: '14' },
{ name: 'Clean freshwater', description: '', sdgNumber: '6' },
{ name: 'Clean oceans', description: '', sdgNumber: '' },
{ name: 'Climate change', description: '', sdgNumber: '13' },
{ name: 'Forest', description: '', sdgNumber: '' },
{ name: 'Soil', description: '', sdgNumber: '' },
],
},
{
name: 'Social',
sortOrder: 2,
interests: [
'Corruption',
'Drinking water',
'Education',
'Gender Equality',
'Health',
'Housing',
'Hunger',
'Indigenous people',
'Inequality',
'Poverty',
'Refugees',
'Urban planning',
'Violence & Abuse',
{ name: 'Corruption', description: '', sdgNumber: '' },
{ name: 'Drinking water', description: '', sdgNumber: '' },
{ name: 'Education', description: '', sdgNumber: '4' },
{ name: 'Gender Equality', description: '', sdgNumber: '5' },
{ name: 'Health', description: '', sdgNumber: '3' },
{ name: 'Housing', description: '', sdgNumber: '' },
{ name: 'Hunger', description: '', sdgNumber: '2' },
{ name: 'Indigenous people', description: '', sdgNumber: '' },
{ name: 'Inequality', description: '', sdgNumber: '10' },
{ name: 'Poverty', description: '', sdgNumber: '1' },
{ name: 'Refugees', description: '', sdgNumber: '' },
{ name: 'Urban planning', description: '', sdgNumber: '' },
{ name: 'Violence & Abuse', description: '', sdgNumber: '' },
],
},
{
name: 'Economy',
sortOrder: 3,
interests: [
'Agriculture',
'Consumption',
'Decent work',
'Economic system',
'Energy',
'Food',
'Production',
'Transportation',
'Waste',
{ name: 'Agriculture', description: '', sdgNumber: '' },
{ name: 'Consumption', description: '', sdgNumber: '12' },
{ name: 'Decent work', description: '', sdgNumber: '8' },
{ name: 'Economic system', description: '', sdgNumber: '' },
{ name: 'Energy', description: '', sdgNumber: '7' },
{ name: 'Food', description: '', sdgNumber: '' },
{ name: 'Production', description: '', sdgNumber: '' },
{ name: 'Transportation', description: '', sdgNumber: '' },
{ name: 'Waste', description: '', sdgNumber: '' },
],
},
],
interests: [{ name: 'Partnerships', description: '', sdgNumber: '17' }],
},
{
name: 'Approach',
sortOrder: 2,
interests: [
'Collaboration',
'Conferences',
'Debate',
'Demonstrations',
'Divestment',
'Education',
'Innovation',
'Investment',
'Knowledge sharing',
'Lobbyism',
'Mobilising',
'Partnerships',
'Petitions',
'Policy making',
'Research',
'Talks',
'Technology',
'Workshops',
'Write opinion pieces',
'Youth involvement',
{ name: 'Collaboration', description: '', sdgNumber: '' },
{ name: 'Conferences', description: '', sdgNumber: '' },
{ name: 'Debate', description: '', sdgNumber: '' },
{ name: 'Demonstrations', description: '', sdgNumber: '' },
{ name: 'Divestment', description: '', sdgNumber: '' },
{ name: 'Education', description: '', sdgNumber: '' },
{ name: 'Innovation', description: '', sdgNumber: '9' },
{ name: 'Investment', description: '', sdgNumber: '' },
{ name: 'Knowledge sharing', description: '', sdgNumber: '' },
{ name: 'Lobbyism', description: '', sdgNumber: '' },
{ name: 'Mobilising', description: '', sdgNumber: '' },
{ name: 'Partnerships', description: '', sdgNumber: '' },
{ name: 'Petitions', description: '', sdgNumber: '' },
{ name: 'Policy making', description: '', sdgNumber: '' },
{ name: 'Research', description: '', sdgNumber: '' },
{ name: 'Talks', description: '', sdgNumber: '' },
{ name: 'Technology', description: '', sdgNumber: '' },
{ name: 'Workshops', description: '', sdgNumber: '' },
{ name: 'Write opinion pieces', description: '', sdgNumber: '' },
{ name: 'Youth involvement', description: '', sdgNumber: '' },
],
},
{
name: 'Tags',
sortOrder: 3,
interests: [
'AntiRacism',
'BalticSea',
'BalticSeaYouthPlatform',
'Banfossilfuels',
'BlackLivesMatter',
'COP26',
'CreativeCircle',
'DoughnutModel',
'Dropoliendan',
'EuropeanGreenDeal',
'Feminism',
'GreenFridayAarhus',
'GreenlightAarhus',
'GreenNewDeal',
'Grøngenstart',
'GrønneAktørerAarhus',
'Klimaplan',
'Kommunalvalg2021',
'MellowDesigns',
'PostGrowth',
'ReGeneration2030',
'SCM',
'Stopbalticpipeline',
'UniteTheWorld',
{ name: 'AntiRacism', description: '', sdgNumber: '' },
{ name: 'BalticSea', description: '', sdgNumber: '' },
{ name: 'BalticSeaYouthPlatform', description: '', sdgNumber: '' },
{ name: 'Banfossilfuels', description: '', sdgNumber: '' },
{ name: 'BlackLivesMatter', description: '', sdgNumber: '' },
{ name: 'COP26', description: '', sdgNumber: '' },
{ name: 'CreativeCircle', description: '', sdgNumber: '' },
{ name: 'DoughnutModel', description: '', sdgNumber: '' },
{ name: 'Dropoliendan', description: '', sdgNumber: '' },
{ name: 'EuropeanGreenDeal', description: '', sdgNumber: '' },
{ name: 'Feminism', description: '', sdgNumber: '' },
{ name: 'GreenFridayAarhus', description: '', sdgNumber: '' },
{ name: 'GreenlightAarhus', description: '', sdgNumber: '' },
{ name: 'GreenNewDeal', description: '', sdgNumber: '' },
{ name: 'Grøngenstart', description: '', sdgNumber: '' },
{ name: 'GrønneAktørerAarhus', description: '', sdgNumber: '' },
{ name: 'Klimaplan', description: '', sdgNumber: '' },
{ name: 'Kommunalvalg2021', description: '', sdgNumber: '' },
{ name: 'MellowDesigns', description: '', sdgNumber: '' },
{ name: 'PostGrowth', description: '', sdgNumber: '' },
{ name: 'ReGeneration2030', description: '', sdgNumber: '' },
{ name: 'SCM', description: '', sdgNumber: '' },
{ name: 'Stopbalticpipeline', description: '', sdgNumber: '' },
{ name: 'UniteTheWorld', description: '', sdgNumber: '' },
],
},
]

export const seedInterests = async (prisma: PrismaClient): Promise<void[]> => {
const upsertInterestType = async (
name: string,
interestType: interestTypeMap,
parent?: interestTypeMap
): Promise<InterestType> => {
const parentId = parent?.id || null
const map = {
name: interestType.name,
sortOrder: interestType.sortOrder,
parentInterestTypeId: parentId,
}
const type = await prisma.interestType.upsert({
create: { name, parentInterestTypeId: parentId },
update: {},
create: map,
update: map,
where: {
nameUniqueForParentInterestType: {
name,
name: interestType.name,
parentInterestTypeId: parentId,
},
},
Expand All @@ -137,13 +155,13 @@ export const seedInterests = async (prisma: PrismaClient): Promise<void[]> => {
}

const upsertInterest = (type: InterestType) => async (
name: string
data: interestMap
): Promise<Interest> => {
const interest = await prisma.interest.upsert({
create: { name, interestTypeId: type.id },
update: {},
create: { ...data, interestTypeId: type.id },
update: { ...data, interestTypeId: type.id },
where: {
nameUniqueForInterestType: { name, interestTypeId: type.id },
nameUniqueForInterestType: { name: data.name, interestTypeId: type.id },
},
})
console.log('Created Interest: ', interest)
Expand All @@ -153,7 +171,7 @@ export const seedInterests = async (prisma: PrismaClient): Promise<void[]> => {
const seedInterestType = (parent?: interestTypeMap) => async (
type: interestTypeMap
) => {
const interestType = await upsertInterestType(type.name, parent)
const interestType = await upsertInterestType(type, parent)

if (type.interests?.length) {
await Promise.all(type.interests.map(upsertInterest(interestType)))
Expand Down
1 change: 1 addition & 0 deletions src/__fixtures__/interest/example-interest-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ import { InterestType } from '@generated/type-graphql'
export const ExampleInterestType: InterestType = {
id: 'e5083f6a-9dab-468c-8fcd-0826ad2bf1e1',
name: 'Example Interest Type',
sortOrder: 0,
}
Loading

0 comments on commit a6804d5

Please sign in to comment.