-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
536f8f7
commit 464a098
Showing
5 changed files
with
128 additions
and
4 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,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 | ||
} | ||
)() | ||
} | ||
} |
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 {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" |
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,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 | ||
} |
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