Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
giovannibaratta committed Feb 19, 2024
1 parent 536f8f7 commit 464a098
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 4 deletions.
44 changes: 44 additions & 0 deletions service/libs/external/src/db/run.repository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import {Injectable, Logger} from "@nestjs/common"
import {DatabaseClient} from "./database-client"
import {CreateRun, RunRepository} from "@libs/service/interfaces/run.interfaces"
import {TaskEither} from "fp-ts/lib/TaskEither"
import {pipe} from "fp-ts/lib/function"
import * as TE from "fp-ts/lib/TaskEither"

@Injectable()
export class RunDbRepository implements RunRepository {
constructor(private readonly dbClient: DatabaseClient) {}

createRun(request: CreateRun): TaskEither<never, string> {
const result = pipe(request, TE.right, TE.chainW(this.persistObjectTask()))
return result
}

private persistObjectTask(): (
request: CreateRun
) => TaskEither<never, string> {
return request =>
TE.tryCatchK(
() =>
this.dbClient.run
.create({
data: {
createdAt: request.baseRun.createdAt,
state: request.baseRun.state,
sourceCodeId: request.sourceCodeId,
planId: request.planId,
id: request.baseRun.id,
updatedAt: request.baseRun.updatedAt
},
select: {
id: true
}
})
.then(result => result.id),
error => {
Logger.error("Error while creating run")
throw error
}
)()
}
}
17 changes: 15 additions & 2 deletions service/libs/external/src/external.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {SourceCodeDbRepository} from "./db/source-code.repository"
import {PLAN_REPOSITORY_TOKEN} from "@libs/service/interfaces/plan.interfaces"
import {PlanDbRepository} from "./db/plan.repository"
import {Config} from "./config/config"
import {RUN_REPOSITORY_TOKEN} from "@libs/service/interfaces/run.interfaces"
import {RunDbRepository} from "./db/run.repository"

const sourceCodeRepository = {
provide: SOURCE_CODE_REPOSITORY_TOKEN,
Expand All @@ -16,9 +18,20 @@ const planRepository = {
useClass: PlanDbRepository
}

const runRepository = {
provide: RUN_REPOSITORY_TOKEN,
useClass: RunDbRepository
}

@Module({
imports: [],
providers: [sourceCodeRepository, planRepository, DatabaseClient, Config],
exports: [sourceCodeRepository, planRepository]
providers: [
sourceCodeRepository,
planRepository,
runRepository,
DatabaseClient,
Config
],
exports: [sourceCodeRepository, planRepository, runRepository]
})
export class ExternalModule {}
14 changes: 14 additions & 0 deletions service/libs/service/src/interfaces/run.interfaces.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import {BaseRun} from "@libs/domain"
import {TaskEither} from "fp-ts/lib/TaskEither"

export interface RunRepository {
createRun(request: CreateRun): TaskEither<never, string>
}

export interface CreateRun {
baseRun: BaseRun
sourceCodeId: string
planId: string
}

export const RUN_REPOSITORY_TOKEN = "RUN_REPOSITORY_TOKEN"
52 changes: 52 additions & 0 deletions service/libs/service/src/run.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import {RunDbRepository} from "@libs/external/db/run.repository"
import {Inject, Injectable, Logger} from "@nestjs/common"
import {randomUUID} from "crypto"
import {Either} from "fp-ts/lib/Either"
import {pipe} from "fp-ts/lib/function"
import * as TE from "fp-ts/lib/TaskEither"

@Injectable()
export class RunService {
constructor(
@Inject("RUN_REPOSITORY_TOKEN")
private readonly runRepository: RunDbRepository
) {}

async createRun(request: CreateRun): Promise<Either<never, string>> {
const createdAt = new Date()

// Wrap in a lambda to preserve the "this" context
const persistRun = (req: CreateRun) =>
this.runRepository.createRun({
sourceCodeId: req.sourceCodeId,
planId: req.planId,
baseRun: {
createdAt,
updatedAt: createdAt,
state: "pending_validation",
id: randomUUID()
}
})

const result = await pipe(
request,
TE.right,
TE.chainW(persistRun),
TE.chainW((result: string) => logCreateResult(result, request))
)()

return result
}
}

const logCreateResult = (result: string, conxtext: CreateRun) => {
Logger.log(
`Created run with id ${result} for source code ${conxtext.sourceCodeId} and plan ${conxtext.planId}`
)
return TE.right(result)
}

export interface CreateRun {
sourceCodeId: string
planId: string
}
5 changes: 3 additions & 2 deletions service/libs/service/src/service.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import {Module} from "@nestjs/common"
import {SourceCodeService} from "./source-code.service"
import {ExternalModule} from "@libs/external/external.module"
import {PlanService} from "./plan.service"
import {RunService} from "./run.service"

@Module({
imports: [ExternalModule],
providers: [SourceCodeService, PlanService],
exports: [SourceCodeService, PlanService]
providers: [SourceCodeService, PlanService, RunService],
exports: [SourceCodeService, PlanService, RunService]
})
export class ServiceModule {}

0 comments on commit 464a098

Please sign in to comment.