-
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.
AB#143 feat: return BaseRun when creating a new run
- Loading branch information
1 parent
f227894
commit d5d075f
Showing
6 changed files
with
72 additions
and
25 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
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 |
---|---|---|
@@ -1,45 +1,82 @@ | ||
import {Injectable, Logger} from "@nestjs/common" | ||
import {DatabaseClient} from "./database-client" | ||
import {Run as PrismaRun} from "@prisma/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" | ||
import {BaseRun} from "@libs/domain" | ||
import {Either, isLeft} from "fp-ts/lib/Either" | ||
import {either} from "fp-ts" | ||
|
||
@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())) | ||
createRun(request: CreateRun): TaskEither<"unknown_run_state", BaseRun> { | ||
const result = pipe( | ||
request, | ||
TE.right, | ||
TE.chainW(this.persistObjectTask()), | ||
TE.chainW(mapToDomain) | ||
) | ||
return result | ||
} | ||
|
||
private persistObjectTask(): ( | ||
request: CreateRun | ||
) => TaskEither<never, string> { | ||
) => TaskEither<never, PrismaRun> { | ||
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, | ||
revision: 0 | ||
}, | ||
select: { | ||
id: true | ||
} | ||
}) | ||
.then(result => result.id), | ||
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, | ||
revision: request.baseRun.revision | ||
} | ||
}), | ||
error => { | ||
Logger.error("Error while creating run") | ||
throw error | ||
} | ||
)() | ||
} | ||
} | ||
|
||
function mapToDomain(run: PrismaRun): TaskEither<"unknown_run_state", BaseRun> { | ||
const eitherState = mapState(run.state) | ||
|
||
if (isLeft(eitherState)) { | ||
return TE.left(eitherState.left) | ||
} | ||
|
||
return TE.right({ | ||
id: run.id, | ||
state: eitherState.right, | ||
createdAt: run.createdAt, | ||
updatedAt: run.updatedAt, | ||
revision: run.revision | ||
}) | ||
} | ||
|
||
function mapState( | ||
rawState: string | ||
): Either<"unknown_run_state", BaseRun["state"]> { | ||
switch (rawState) { | ||
case "pending_validation": | ||
return either.right("pending_validation") | ||
case "pending_approval": | ||
return either.right("pending_approval") | ||
case "approved": | ||
return either.right("approved") | ||
case "rejected": | ||
return either.right("rejected") | ||
default: | ||
return either.left("unknown_run_state") | ||
} | ||
} |
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