-
Notifications
You must be signed in to change notification settings - Fork 3
Implement GET /claims
#82
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
SidneyNemzer
wants to merge
5
commits into
BuildTheEarth:api/v2
Choose a base branch
from
SidneyNemzer:api-v2/claims
base: api/v2
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
68c2671
fix(api/v2): allow app to start without cachet url/token
SidneyNemzer b988d7b
fix(api/v2): require JWT_SECRET for app to start
SidneyNemzer f219e07
feat(api/v2): :sparkles: implement GET /claims
SidneyNemzer b02e641
fix(api/claims): :bug: Add prisma service to claims module
Nudelsuppe42 7a1ba9f
feat(api/v2): refactor to use authenticated team as default filter
SidneyNemzer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
18 changes: 18 additions & 0 deletions
18
apps/api-v2/src/common/decorators/optional-auth.decorator.ts
This file contains hidden or 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,18 @@ | ||
| import { SetMetadata } from "@nestjs/common"; | ||
|
|
||
| export const IS_AUTH_OPTIONAL_KEY = "isAuthOptional"; | ||
|
|
||
| /** | ||
| * Decorator that makes authentication optional for a route. By default, routes | ||
| * require a valid JWT token. | ||
| * | ||
| * @example | ||
| * | ||
| * @OptionalAuth() | ||
| * @Get('optional-auth-endpoint') | ||
| * async getOptionalAuthData(@Request() req: Request) { | ||
| * // req.token may be undefined if no valid token is provided | ||
| * } | ||
| * | ||
| */ | ||
| export const OptionalAuth = () => SetMetadata(IS_AUTH_OPTIONAL_KEY, true); |
This file contains hidden or 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 hidden or 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 hidden or 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,51 @@ | ||
| import { Controller, Get, Req } from "@nestjs/common"; | ||
| import { ClaimsService } from "./claims.service"; | ||
| import { Filtered } from "src/common/decorators/filtered.decorator"; | ||
| import { ApiBearerAuth, ApiOperation } from "@nestjs/swagger"; | ||
| import { ClaimDto } from "./dto/claim.dto"; | ||
| import { ApiDefaultResponse } from "src/common/decorators/api-response.decorator"; | ||
| import { Filter, FilterParams } from "src/common/decorators/filter.decorator"; | ||
| import { Request } from "express"; | ||
| import { OptionalAuth } from "src/common/decorators/optional-auth.decorator"; | ||
|
|
||
| @Controller("claims") | ||
| export class ClaimsController { | ||
| constructor(private readonly claimsService: ClaimsService) {} | ||
|
|
||
| @Get() | ||
| @OptionalAuth() | ||
| @ApiBearerAuth() | ||
| @ApiOperation({ | ||
| summary: "Get All Claims", | ||
| description: | ||
| "Returns all claims for the given team. If no team is specified, returns claims for the authenticated team.", | ||
| }) | ||
| @Filtered({ | ||
| fields: [ | ||
| { name: "finished", required: false, type: Boolean }, | ||
| { name: "active", required: false, type: Boolean }, | ||
| { name: "team", required: false, type: String }, | ||
| { name: "slug", required: false, type: Boolean }, | ||
| ], | ||
| }) | ||
| @ApiDefaultResponse(ClaimDto, { | ||
| description: "List of claims matching the filters.", | ||
| isArray: true, | ||
| }) | ||
| findAll(@Filter() filter: FilterParams, @Req() req: Request) { | ||
| const { team, slug, ...otherFilters }: { team?: string; slug?: boolean } = | ||
| filter.filter; | ||
|
|
||
| const teamFilter: { | ||
| buildTeamId?: string; | ||
| buildTeam?: { slug: string }; | ||
| } = (() => { | ||
| if (!team && req.token) return { buildTeamId: req.token.id }; | ||
| if (!team) return {}; | ||
| if (slug) return { buildTeam: { slug: team } }; | ||
| return { buildTeamId: team }; | ||
| })(); | ||
|
|
||
| return this.claimsService.findAll({ ...otherFilters, ...teamFilter }); | ||
| } | ||
| } | ||
This file contains hidden or 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,10 @@ | ||
| import { Module } from "@nestjs/common"; | ||
| import { PrismaService } from "src/common/db/prisma.service"; | ||
| import { ClaimsController } from "./claims.controller"; | ||
| import { ClaimsService } from "./claims.service"; | ||
|
|
||
| @Module({ | ||
| controllers: [ClaimsController], | ||
| providers: [ClaimsService, PrismaService], | ||
| }) | ||
| export class ClaimsModule {} |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Potentially add pagination and sorting like in |
This file contains hidden or 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,18 @@ | ||
| import { Injectable } from "@nestjs/common"; | ||
| import { FilterParams } from "src/common/decorators/filter.decorator"; | ||
| import { PrismaService } from "src/common/db/prisma.service"; | ||
|
|
||
| @Injectable() | ||
| export class ClaimsService { | ||
| constructor(private readonly prisma: PrismaService) {} | ||
|
|
||
| async findAll(filter: FilterParams["filter"]) { | ||
| return this.prisma.claim.findMany({ | ||
| where: filter, | ||
| include: { | ||
| _count: { select: { builders: true, images: true } }, | ||
| images: { select: { id: true, name: true, hash: true } }, | ||
| }, | ||
| }); | ||
| } | ||
| } |
This file contains hidden or 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,28 @@ | ||
| export class ClaimDto { | ||
| id: string; | ||
| ownerId: string | null; | ||
| area: string[]; | ||
| center: string | null; | ||
| size: number; | ||
| active: boolean; | ||
| finished: boolean; | ||
| buildTeamId: string; | ||
| name: string; | ||
| createdAt: string; | ||
| externalId: string | null; | ||
| description: string | null; | ||
| buildings: number; | ||
| city: string | null; | ||
| osmName: string | null; | ||
|
|
||
| _count: { | ||
| builders: number; | ||
| images: number; | ||
| }; | ||
|
|
||
| images: { | ||
| id: string; | ||
| name: string; | ||
| hash: string; | ||
| }[]; | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.