Skip to content

Commit

Permalink
AB#149 feat: add Run to the domain
Browse files Browse the repository at this point in the history
  • Loading branch information
giovannibaratta committed Feb 19, 2024
1 parent 0e42736 commit 536f8f7
Show file tree
Hide file tree
Showing 9 changed files with 132 additions and 0 deletions.
Binary file not shown.
Binary file not shown.
1 change: 1 addition & 0 deletions service/libs/domain/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./source-code"
export * from "./s3"
export * from "./plan"
export * from "./run"
26 changes: 26 additions & 0 deletions service/libs/domain/src/run.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import {Plan} from "./plan"
import {SourceCode} from "./source-code"

export interface BaseRun {
readonly id: string
readonly state:
| "pending_validation"
| "pending_approval"
| "approved"
| "rejected"
createdAt: Date
updatedAt: Date
}

export interface Run extends BaseRun {
readonly sourceCode: SourceCode
readonly plan: Plan
}

export function isRunApproved(run: BaseRun): boolean {
return run.state === "approved"
}

export function isRunInProgress(run: BaseRun): boolean {
return run.state !== "approved" && run.state !== "rejected"
}
61 changes: 61 additions & 0 deletions service/libs/domain/test/run.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import {BaseRun, isRunApproved, isRunInProgress} from "@libs/domain"
import {generateMockRun} from "@libs/testing"

describe("isRunApproved", () => {
it("should return true if the run state is 'approved'", () => {
// Given
const run: BaseRun = generateMockRun({state: "approved"})

// When
const result = isRunApproved(run)

// Expect
expect(result).toBe(true)
})

it("should return false if the run state is not 'approved'", () => {
// Given
const run: BaseRun = generateMockRun({state: "pending_approval"})

// When
const result = isRunApproved(run)

// Expect
expect(result).toBe(false)
})
})

describe("isRunInProgress", () => {
it("should return true if the run state is not 'approved' or 'rejected'", () => {
// Given
const run: BaseRun = generateMockRun({state: "pending_approval"})

// When
const result = isRunInProgress(run)

// Expect
expect(result).toBe(true)
})

it("should return false if the run state is 'approved'", () => {
// Given
const run: BaseRun = generateMockRun({state: "approved"})

// When
const result = isRunInProgress(run)

// Expect
expect(result).toBe(false)
})

it("should return false if the run state is 'rejected'", () => {
// Given
const run: BaseRun = generateMockRun({state: "rejected"})

// When
const result = isRunInProgress(run)

// Expect
expect(result).toBe(false)
})
})
1 change: 1 addition & 0 deletions service/libs/testing/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./mocks/run"
25 changes: 25 additions & 0 deletions service/libs/testing/src/mocks/run.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import {BaseRun} from "@libs/domain"
// eslint-disable-next-line node/no-unpublished-import
import {Chance} from "chance"

const random = new Chance()

export function generateMockRun(overrides: Partial<BaseRun>): BaseRun {
const baseObj: BaseRun = {
id: random.guid(),
state: random.pickone([
"pending_validation",
"pending_approval",
"approved",
"rejected"
]),
createdAt: new Date(),
updatedAt: new Date(),
...overrides
}

return {
...baseObj,
...overrides
}
}
2 changes: 2 additions & 0 deletions service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,14 @@
"@nestjs/cli": "10.3.0",
"@nestjs/schematics": "10.1.0",
"@nestjs/testing": "10.2.8",
"@types/chance": "1.1.6",
"@types/express": "4.17.21",
"@types/jest": "29.5.8",
"@types/node": "18.18.9",
"@types/supertest": "6.0.2",
"@typescript-eslint/eslint-plugin": "6.10.0",
"@typescript-eslint/parser": "6.10.0",
"chance": "1.1.11",
"dotenv-cli": "7.3.0",
"eslint": "8.53.0",
"eslint-config-prettier": "8.10.0",
Expand Down
16 changes: 16 additions & 0 deletions service/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1274,6 +1274,13 @@ __metadata:
languageName: node
linkType: hard

"@types/chance@npm:1.1.6":
version: 1.1.6
resolution: "@types/chance@npm:1.1.6"
checksum: f4366f1b3144d143af3e6f0fad2ed1db7b9bdfa7d82d40944e9619d57fe7e6b60e8c1452f47a8ededa6b2188932879518628ecd9aac81c40384ded39c26338ba
languageName: node
linkType: hard

"@types/connect@npm:*":
version: 3.4.38
resolution: "@types/connect@npm:3.4.38"
Expand Down Expand Up @@ -2463,6 +2470,13 @@ __metadata:
languageName: node
linkType: hard

"chance@npm:1.1.11":
version: 1.1.11
resolution: "chance@npm:1.1.11"
checksum: d76cc76dbcb837f051e6080d94be8bdcd07559d52077854f614c7081062fee10d4c3b508f6ae4b70303dac000422ea35160b01de165fcd176a01f67ea4b2cef5
languageName: node
linkType: hard

"char-regex@npm:^1.0.2":
version: 1.0.2
resolution: "char-regex@npm:1.0.2"
Expand Down Expand Up @@ -6714,12 +6728,14 @@ __metadata:
"@nestjs/schematics": "npm:10.1.0"
"@nestjs/testing": "npm:10.2.8"
"@prisma/client": "npm:5.9.0"
"@types/chance": "npm:1.1.6"
"@types/express": "npm:4.17.21"
"@types/jest": "npm:29.5.8"
"@types/node": "npm:18.18.9"
"@types/supertest": "npm:6.0.2"
"@typescript-eslint/eslint-plugin": "npm:6.10.0"
"@typescript-eslint/parser": "npm:6.10.0"
chance: "npm:1.1.11"
dotenv-cli: "npm:7.3.0"
eslint: "npm:8.53.0"
eslint-config-prettier: "npm:8.10.0"
Expand Down

0 comments on commit 536f8f7

Please sign in to comment.