Skip to content

Commit 8252336

Browse files
authored
Merge pull request #58 from zama-zws/32-list-apps-on-dashboard
feat: list dapps
2 parents 7d544e7 + 94d1285 commit 8252336

File tree

18 files changed

+218
-41
lines changed

18 files changed

+218
-41
lines changed

apps/back/src/dapps/domain/entities/dapp.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ export class DApp extends Entity<DAppProps> implements Readonly<DAppProps> {
2525
: fail(validationError(check.error.message))
2626
}
2727

28+
static parseArray(data: unknown[]): Result<DApp[], AppError> {
29+
const res = data.map(DApp.parse)
30+
return res.every(dapp => dapp.isOk())
31+
? ok(res.reduce<DApp[]>((acc, dapp) => [...acc, dapp.value], []))
32+
: fail(res.find(dapp => dapp.isFail())!.error)
33+
}
34+
2835
get id() {
2936
return this.get('id')
3037
}

apps/back/src/dapps/domain/repositories/dapp.repository.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ export abstract class DAppRepository {
99
id: string,
1010
userId: string,
1111
): Task<DApp, AppError>
12+
abstract findAllByTeamId(teamId: string): Task<DApp[], AppError>
1213
}

apps/back/src/dapps/infra/dapps.module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Module } from '@nestjs/common'
22
import { DappsResolver } from './dapps.resolver'
33
import { DatabaseModule } from '@/infra/database/database.module'
44
import { CreateDapp } from '@/dapps/use-cases/create-dapp.use-case'
5-
import { UpdateDapp } from '../use-cases/update-dapp.use-case'
5+
import { UpdateDapp } from '@/dapps/use-cases/update-dapp.use-case'
66
import { GetTeamById } from '@/users/use-cases/get-team-by-id.use-case'
77

88
@Module({

apps/back/src/dapps/infra/dapps.resolver.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { CurrentUser } from '@/auth/infra/decorators/current-user'
1010
import { JwtAuthGuard } from '@/auth/infra/guards/jwt-auth-guard'
1111
import { User } from '@/users/domain/entities/user'
1212
import { TeamId } from '@/users/domain/entities/value-objects'
13+
import { TeamType } from '@/users/infra/types/team.type'
1314

1415
@Resolver(() => DappType)
1516
export class DappsResolver {
@@ -31,7 +32,7 @@ export class DappsResolver {
3132
return this.updateDappUC.execute({ dapp: input, user }).toPromise()
3233
}
3334

34-
@ResolveField()
35+
@ResolveField(() => TeamType, { name: 'team' })
3536
async team(@Parent() dapp: DappType) {
3637
const { teamId } = dapp
3738
return this.getTeamByIdUC.execute(new TeamId(teamId)).toPromise()
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { Injectable } from '@nestjs/common'
2+
import type { AppError, UseCase } from 'utils'
3+
import { Task } from 'utils'
4+
import { DApp } from '../domain/entities/dapp'
5+
import { DAppRepository } from '../domain/repositories/dapp.repository'
6+
import { TeamId } from '@/users/domain/entities/value-objects'
7+
8+
@Injectable()
9+
export class GetDappsByTeamId implements UseCase<TeamId, DApp[]> {
10+
constructor(private readonly dappRepository: DAppRepository) {}
11+
execute(teamId: TeamId): Task<DApp[], AppError> {
12+
return this.dappRepository.findAllByTeamId(teamId.value)
13+
}
14+
}

apps/back/src/infra/database/repositories/prisma-dapp.repository.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,14 @@ export class PrismaDAppRepository extends DAppRepository {
4848
.catch(err => reject(unknownError(String(err))))
4949
}).chain(props => DApp.parse(props).async())
5050
}
51+
findAllByTeamId = (teamId: string): Task<DApp[], AppError> => {
52+
return new Task<unknown[], AppError>((resolve, reject) => {
53+
this.db.dapp
54+
.findMany({
55+
where: { teamId },
56+
})
57+
.then(resolve)
58+
.catch(err => reject(unknownError(String(err))))
59+
}).chain(dapps => DApp.parseArray(dapps).async())
60+
}
5161
}

apps/back/src/infra/graphql/schema.gql

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,31 @@
22
# THIS FILE WAS AUTOMATICALLY GENERATED (DO NOT MODIFY)
33
# ------------------------------------------------------
44

5+
type Dapp {
6+
id: ID!
7+
name: String!
8+
status: DappStatus!
9+
address: String
10+
teamId: String! @deprecated(reason: "Do not use this, it shall go away when I find a way to make it disappear")
11+
team: Team!
12+
}
13+
14+
enum DappStatus {
15+
"""Still being worked on"""
16+
DRAFT
17+
18+
"""We are deploying it"""
19+
DEPLOYING
20+
21+
"""You can use it now"""
22+
LIVE
23+
DELETED @deprecated(reason: "Not implmented yet")
24+
}
25+
526
type Team {
627
id: ID!
728
name: String!
29+
dapps: [Dapp!]!
830
}
931

1032
type User {
@@ -28,27 +50,6 @@ type Invitation {
2850
expiresAt: Float!
2951
}
3052

31-
type Dapp {
32-
id: ID!
33-
name: String!
34-
status: DappStatus!
35-
address: String
36-
teamId: String! @deprecated(reason: "Do not use this, it shall go away when I find a way to make it disappear")
37-
team: Team!
38-
}
39-
40-
enum DappStatus {
41-
"""Still being worked on"""
42-
DRAFT
43-
44-
"""We are deploying it"""
45-
DEPLOYING
46-
47-
"""You can use it now"""
48-
LIVE
49-
DELETED @deprecated(reason: "Not implmented yet")
50-
}
51-
5253
type Query {
5354
me: User!
5455
invitation(token: String!): Invitation!
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { Resolver, ResolveField, Parent } from '@nestjs/graphql'
2+
import { TeamId, UserId } from '../domain/entities/value-objects'
3+
import { DappType } from '@/dapps/infra/types/dapp.type'
4+
import { TeamType } from './types/team.type'
5+
import { GetDappsByTeamId } from '@/dapps/use-cases/get-dapps-by-team-id.use-case'
6+
7+
@Resolver(() => TeamType)
8+
export class TeamsResolver {
9+
constructor(private readonly getDappsByTeamUC: GetDappsByTeamId) {}
10+
11+
@ResolveField(() => [DappType], { name: 'dapps' })
12+
async dapps(@Parent() team: TeamType) {
13+
return this.getDappsByTeamUC.execute(new TeamId(team.id)).toPromise()
14+
}
15+
}

apps/back/src/users/infra/types/team.type.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { DappType } from '@/dapps/infra/types/dapp.type'
12
import { Field, ID, ObjectType } from '@nestjs/graphql'
23

34
@ObjectType('Team')
@@ -7,4 +8,7 @@ export class TeamType {
78

89
@Field({ nullable: false })
910
name: string
11+
12+
@Field(() => [DappType], { nullable: false })
13+
dapps: DappType[]
1014
}

apps/back/src/users/infra/users.module.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,18 @@ import { GetUserById } from '@/users/use-cases/get-user-by-id.use-case'
55
import { GetTeamById } from '@/users/use-cases/get-team-by-id.use-case'
66
import { GetTeamsByUserId } from '@/users/use-cases/get-teams-by-user-id.use-case'
77
import { AuthModule } from '@/auth/infra/auth.module'
8+
import { GetDappsByTeamId } from '@/dapps/use-cases/get-dapps-by-team-id.use-case'
9+
import { TeamsResolver } from './teams.resolver'
810

911
@Module({
1012
imports: [DatabaseModule, AuthModule],
11-
providers: [UsersResolver, GetUserById, GetTeamsByUserId, GetTeamById],
13+
providers: [
14+
UsersResolver,
15+
TeamsResolver,
16+
GetUserById,
17+
GetTeamsByUserId,
18+
GetTeamById,
19+
GetDappsByTeamId,
20+
],
1221
})
1322
export class UsersModule {}

0 commit comments

Comments
 (0)