-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
Merge pull request #568 from Klimatbyran/staging
Update production with latest changes: Nullable fields + returning Ids and schema/types cleanup
Showing
17 changed files
with
627 additions
and
535 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,324 @@ | ||
import { Prisma } from '@prisma/client' | ||
|
||
export const emissionsArgs = { | ||
include: { | ||
scope1: { select: { id: true } }, | ||
scope2: { select: { id: true } }, | ||
scope3: { select: { id: true } }, | ||
biogenicEmissions: { select: { id: true } }, | ||
scope1And2: { select: { id: true } }, | ||
statedTotalEmissions: { select: { id: true } }, | ||
}, | ||
} satisfies Prisma.EmissionsDefaultArgs | ||
|
||
export const economyArgs = { | ||
include: { | ||
employees: { select: { id: true } }, | ||
turnover: { select: { id: true } }, | ||
}, | ||
} satisfies Prisma.EconomyDefaultArgs | ||
|
||
export const reportingPeriodArgs = { | ||
include: { | ||
emissions: emissionsArgs, | ||
economy: economyArgs, | ||
company: { select: { wikidataId: true } }, | ||
}, | ||
} satisfies Prisma.ReportingPeriodDefaultArgs | ||
|
||
const metadataArgs = { | ||
orderBy: { | ||
updatedAt: 'desc' as const, | ||
}, | ||
take: 1, | ||
select: { | ||
id: true, | ||
comment: true, | ||
source: true, | ||
updatedAt: true, | ||
user: { | ||
select: { | ||
name: true, | ||
}, | ||
}, | ||
verifiedBy: { | ||
select: { | ||
name: true, | ||
}, | ||
}, | ||
} satisfies Prisma.MetadataDefaultArgs['select'], | ||
} | ||
|
||
const minimalMetadataArgs = { | ||
orderBy: { | ||
updatedAt: 'desc' as const, | ||
}, | ||
take: 1, | ||
select: { | ||
verifiedBy: { | ||
select: { | ||
name: true, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
export const detailedCompanyArgs = { | ||
select: { | ||
wikidataId: true, | ||
name: true, | ||
description: true, | ||
reportingPeriods: { | ||
select: { | ||
id: true, | ||
startDate: true, | ||
endDate: true, | ||
reportURL: true, | ||
economy: { | ||
select: { | ||
id: true, | ||
turnover: { | ||
select: { | ||
id: true, | ||
value: true, | ||
currency: true, | ||
metadata: metadataArgs, | ||
}, | ||
}, | ||
employees: { | ||
select: { | ||
id: true, | ||
value: true, | ||
unit: true, | ||
metadata: metadataArgs, | ||
}, | ||
}, | ||
}, | ||
}, | ||
emissions: { | ||
select: { | ||
id: true, | ||
scope1: { | ||
select: { | ||
id: true, | ||
total: true, | ||
unit: true, | ||
metadata: metadataArgs, | ||
}, | ||
}, | ||
scope2: { | ||
select: { | ||
id: true, | ||
lb: true, | ||
mb: true, | ||
unknown: true, | ||
unit: true, | ||
metadata: metadataArgs, | ||
}, | ||
}, | ||
scope3: { | ||
select: { | ||
id: true, | ||
statedTotalEmissions: { | ||
select: { | ||
id: true, | ||
total: true, | ||
unit: true, | ||
metadata: metadataArgs, | ||
}, | ||
}, | ||
categories: { | ||
select: { | ||
id: true, | ||
category: true, | ||
total: true, | ||
unit: true, | ||
metadata: metadataArgs, | ||
}, | ||
orderBy: { | ||
category: 'asc', | ||
}, | ||
}, | ||
metadata: metadataArgs, | ||
}, | ||
}, | ||
biogenicEmissions: { | ||
select: { | ||
id: true, | ||
total: true, | ||
unit: true, | ||
metadata: metadataArgs, | ||
}, | ||
}, | ||
scope1And2: { | ||
select: { | ||
id: true, | ||
total: true, | ||
unit: true, | ||
metadata: metadataArgs, | ||
}, | ||
}, | ||
statedTotalEmissions: { | ||
select: { | ||
id: true, | ||
total: true, | ||
unit: true, | ||
metadata: metadataArgs, | ||
}, | ||
}, | ||
}, | ||
}, | ||
metadata: metadataArgs, | ||
}, | ||
orderBy: { | ||
startDate: 'desc', | ||
}, | ||
}, | ||
industry: { | ||
select: { | ||
id: true, | ||
industryGics: { | ||
select: { | ||
sectorCode: true, | ||
groupCode: true, | ||
industryCode: true, | ||
subIndustryCode: true, | ||
}, | ||
}, | ||
metadata: metadataArgs, | ||
}, | ||
}, | ||
goals: { | ||
select: { | ||
id: true, | ||
description: true, | ||
year: true, | ||
baseYear: true, | ||
target: true, | ||
metadata: metadataArgs, | ||
}, | ||
orderBy: { | ||
year: 'desc', | ||
}, | ||
}, | ||
initiatives: { | ||
select: { | ||
id: true, | ||
title: true, | ||
description: true, | ||
year: true, | ||
scope: true, | ||
metadata: metadataArgs, | ||
}, | ||
orderBy: { | ||
year: 'desc', | ||
}, | ||
}, | ||
}, | ||
} satisfies Prisma.CompanyDefaultArgs | ||
|
||
export const companyListArgs = { | ||
select: { | ||
wikidataId: true, | ||
name: true, | ||
description: true, | ||
reportingPeriods: { | ||
select: { | ||
startDate: true, | ||
endDate: true, | ||
reportURL: true, | ||
economy: { | ||
select: { | ||
turnover: { | ||
select: { | ||
value: true, | ||
currency: true, | ||
metadata: minimalMetadataArgs, | ||
}, | ||
}, | ||
employees: { | ||
select: { | ||
value: true, | ||
unit: true, | ||
metadata: minimalMetadataArgs, | ||
}, | ||
}, | ||
}, | ||
}, | ||
emissions: { | ||
select: { | ||
scope1: { | ||
select: { | ||
total: true, | ||
unit: true, | ||
metadata: minimalMetadataArgs, | ||
}, | ||
}, | ||
scope2: { | ||
select: { | ||
lb: true, | ||
mb: true, | ||
unknown: true, | ||
unit: true, | ||
metadata: minimalMetadataArgs, | ||
}, | ||
}, | ||
scope3: { | ||
select: { | ||
statedTotalEmissions: { | ||
select: { | ||
total: true, | ||
unit: true, | ||
metadata: minimalMetadataArgs, | ||
}, | ||
}, | ||
categories: { | ||
select: { | ||
category: true, | ||
total: true, | ||
unit: true, | ||
metadata: minimalMetadataArgs, | ||
}, | ||
orderBy: { | ||
category: 'asc', | ||
}, | ||
}, | ||
metadata: minimalMetadataArgs, | ||
}, | ||
}, | ||
scope1And2: { | ||
select: { | ||
total: true, | ||
unit: true, | ||
metadata: minimalMetadataArgs, | ||
}, | ||
}, | ||
statedTotalEmissions: { | ||
select: { | ||
total: true, | ||
unit: true, | ||
metadata: minimalMetadataArgs, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
orderBy: { | ||
startDate: 'desc', | ||
}, | ||
}, | ||
industry: { | ||
select: { | ||
industryGics: { | ||
select: { | ||
sectorCode: true, | ||
groupCode: true, | ||
industryCode: true, | ||
subIndustryCode: true, | ||
}, | ||
}, | ||
metadata: minimalMetadataArgs, | ||
}, | ||
}, | ||
}, | ||
} satisfies Prisma.CompanyDefaultArgs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { z } from 'zod' | ||
|
||
export const wikidataIdSchema = z.string().regex(/Q\d+/) | ||
|
||
export const wikidataIdParamSchema = z.object({ wikidataId: wikidataIdSchema }) | ||
|
||
export const garboEntityIdSchema = z.object({ id: z.string() }) | ||
|
||
/** | ||
* This allows reporting periods like 2022-2023 | ||
*/ | ||
export const yearSchema = z.string().regex(/\d{4}(?:-\d{4})?/) | ||
|
||
export const yearParamSchema = z.object({ year: yearSchema }) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from './common' | ||
export * from './request' | ||
export * from './response' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
import { z } from 'zod' | ||
import { wikidataIdSchema } from './common' | ||
|
||
const createMetadataSchema = z.object({ | ||
metadata: z | ||
.object({ | ||
source: z.string().optional(), | ||
comment: z.string().optional(), | ||
}) | ||
.optional(), | ||
}) | ||
|
||
export const postCompanyBodySchema = z.object({ | ||
wikidataId: wikidataIdSchema, | ||
name: z.string(), | ||
description: z.string().optional(), | ||
url: z.string().url().optional(), | ||
internalComment: z.string().optional(), | ||
tags: z.array(z.string()).optional(), | ||
}) | ||
|
||
export const reportingPeriodBodySchema = z | ||
.object({ | ||
startDate: z.coerce.date(), | ||
endDate: z.coerce.date(), | ||
reportURL: z.string().optional(), | ||
}) | ||
.refine(({ startDate, endDate }) => startDate.getTime() < endDate.getTime(), { | ||
message: 'startDate must be earlier than endDate', | ||
}) | ||
|
||
export const goalSchema = z.object({ | ||
description: z.string(), | ||
year: z.string().optional(), | ||
target: z.number().optional(), | ||
baseYear: z.string().optional(), | ||
}) | ||
|
||
export const postGoalSchema = z | ||
.object({ | ||
goal: goalSchema, | ||
}) | ||
.merge(createMetadataSchema) | ||
|
||
export const postGoalsSchema = z | ||
.object({ | ||
goals: z.array(goalSchema), | ||
}) | ||
.merge(createMetadataSchema) | ||
|
||
export const initiativeSchema = z.object({ | ||
title: z.string(), | ||
description: z.string().optional(), | ||
year: z.string().optional(), | ||
scope: z.string().optional(), | ||
}) | ||
|
||
export const postInitiativeSchema = z | ||
.object({ initiative: initiativeSchema }) | ||
.merge(createMetadataSchema) | ||
|
||
export const postInitiativesSchema = z | ||
.object({ | ||
initiatives: z.array(initiativeSchema), | ||
}) | ||
.merge(createMetadataSchema) | ||
|
||
export const industrySchema = z.object({ | ||
subIndustryCode: z.string(), | ||
}) | ||
|
||
export const postIndustrySchema = z | ||
.object({ industry: industrySchema }) | ||
.merge(createMetadataSchema) | ||
|
||
export const statedTotalEmissionsSchema = z | ||
.object({ total: z.number() }) | ||
.optional() | ||
|
||
export const emissionsSchema = z | ||
.object({ | ||
scope1: z | ||
.object({ | ||
total: z.number(), | ||
}) | ||
.optional(), | ||
scope2: z | ||
.object({ | ||
mb: z | ||
.number({ description: 'Market-based scope 2 emissions' }) | ||
.optional(), | ||
lb: z | ||
.number({ description: 'Location-based scope 2 emissions' }) | ||
.optional(), | ||
unknown: z | ||
.number({ description: 'Unspecified Scope 2 emissions' }) | ||
.optional(), | ||
}) | ||
.refine( | ||
({ mb, lb, unknown }) => | ||
mb !== undefined || lb !== undefined || unknown !== undefined, | ||
{ | ||
message: | ||
'At least one property of `mb`, `lb` and `unknown` must be defined if scope2 is provided', | ||
} | ||
) | ||
.optional(), | ||
scope3: z | ||
.object({ | ||
categories: z | ||
.array( | ||
z.object({ | ||
category: z.number().int().min(1).max(16), | ||
total: z.number(), | ||
}) | ||
) | ||
.optional(), | ||
statedTotalEmissions: statedTotalEmissionsSchema, | ||
}) | ||
.optional(), | ||
biogenic: z.object({ total: z.number() }).optional(), | ||
statedTotalEmissions: statedTotalEmissionsSchema, | ||
scope1And2: z.object({ total: z.number() }).optional(), | ||
}) | ||
.optional() | ||
|
||
export const economySchema = z | ||
.object({ | ||
turnover: z | ||
.object({ | ||
value: z.number().optional(), | ||
currency: z.string().optional(), | ||
}) | ||
.optional(), | ||
employees: z | ||
.object({ | ||
value: z.number().optional(), | ||
unit: z.string().optional(), | ||
}) | ||
.optional(), | ||
}) | ||
.optional() | ||
|
||
export const postEconomySchema = z.object({ | ||
economy: economySchema, | ||
}) | ||
|
||
export const postEmissionsSchema = z.object({ | ||
emissions: emissionsSchema, | ||
}) | ||
|
||
export const reportingPeriodSchema = z | ||
.object({ | ||
startDate: z.coerce.date(), | ||
endDate: z.coerce.date(), | ||
reportURL: z.string().optional(), | ||
emissions: emissionsSchema, | ||
economy: economySchema, | ||
}) | ||
.refine(({ startDate, endDate }) => startDate.getTime() < endDate.getTime(), { | ||
message: 'startDate must be earlier than endDate', | ||
}) | ||
|
||
export const postReportingPeriodsSchema = z | ||
.object({ | ||
reportingPeriods: z.array(reportingPeriodSchema), | ||
}) | ||
.merge(createMetadataSchema) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters