Skip to content

Commit

Permalink
Merge branch 'main' into AJP/reviews-findByUuid
Browse files Browse the repository at this point in the history
  • Loading branch information
AJaccP authored Jun 14, 2024
2 parents bfa3202 + 02daa66 commit 0f2ab2c
Show file tree
Hide file tree
Showing 14 changed files with 99 additions and 8 deletions.
15 changes: 15 additions & 0 deletions .github/ci-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import type { Config } from "drizzle-kit";
import dotenv from "dotenv";

dotenv.config();

export default {
schema: "./db/schema.ts",
out: "./db/migrations",
dialect: "sqlite",
driver: "better-sqlite",
dbCredentials: {
url: process.env.DATABASE_URL,
},
verbose: true,
} as Config;
22 changes: 22 additions & 0 deletions .github/workflows/workflow.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: CI
'on':
- push
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Install modules
run: |
npm install
npm install dotenv-cli
- name: Run Prettier
run: |
npm run check
- name: Set up db
run: |
echo -e 'PORT=3000\nDATABASE_URL="./db/test.db"' > .env.test
npx dotenv -e .env.test -- drizzle-kit push --config=.github/ci-config.ts
- name: Run tests
run: |
npm run test
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ npm install
npm install -g dotenv-cli
```

5. Add a `.env` and `.env.test` file it your root with the following configurations:
5. Add a `.env` and `.env.test` file in your root with the following configurations:

```bash
PORT=3000
Expand Down
5 changes: 5 additions & 0 deletions app/challenges-platform/models/Challenge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export enum Evaluation {
MANUAL,
AUTOMATIC,
}

export class Challenge {
id: number;
uuid: string;
Expand All @@ -16,6 +17,7 @@ export class Challenge {
format: Format;
evaluation: Evaluation;
points: number;
deleted: boolean;

constructor({
id,
Expand All @@ -25,6 +27,7 @@ export class Challenge {
format,
evaluation,
points,
deleted,
}: {
id: number;
uuid: string;
Expand All @@ -33,6 +36,7 @@ export class Challenge {
format: Format;
evaluation: Evaluation;
points: number;
deleted: boolean;
}) {
this.id = id;
this.uuid = uuid;
Expand All @@ -41,5 +45,6 @@ export class Challenge {
this.format = format;
this.evaluation = evaluation;
this.points = points;
this.deleted = deleted;
}
}
1 change: 1 addition & 0 deletions app/challenges-platform/models/Transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export class BaseTransformer extends Transformer {
format: Format.TEXT,
points: payload.points,
evaluation: Evaluation.MANUAL,
deleted: payload.deleted,
});
return challenge;
}
Expand Down
19 changes: 15 additions & 4 deletions app/challenges-platform/services/challenges-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,19 @@ export const update = async (): Promise<Result<Challenge, Error>> => {
return Err(new Error("Not implemented"));
};

export const destroy = async (): Promise<Result<Challenge, Error>> => {
// TODO: should mark a challenge as deleted (soft delete)
// TODO: add a "deleted" boolean column to the challenges table
return Err(new Error("Not implemented"));
export const destroy = async (
id: string,
): Promise<Result<Challenge, Error>> => {
try {
const result = await db
.update(challenges)
.set({ deleted: true })
.where(eq(challenges.uuid, id))
.returning();
const transformer = challengesPlatform.findTransformer(result[0].type);
const challenge = transformer.newChallenge(result[0]);
return Ok(challenge);
} catch (e) {
return Err(new Error("Failed to mark challenge as deleted"));
}
};
7 changes: 7 additions & 0 deletions app/challenges-platform/services/submissions-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,15 @@ export const findByUuid = async (
if (!challengeResult.ok) {
return Err(new Error("Failed to find challenge"));
}
<<<<<<< AJP/reviews-findByUuid

const participantResult = await ParticipantsService.findById(record.participantId);
=======
if (challengeResult.val.deleted === true) {
return Err(new Error("Challenge is deleted"));
}
const participantResult = await ParticipantsService.findByUuid(participantId);
>>>>>>> main
if (!participantResult.ok) {
return Err(new Error("Failed to find participant"));
}
Expand Down
1 change: 1 addition & 0 deletions app/event-management/models/FlagChallenge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export class FlagChallenge extends Challenge {
format,
points,
evaluation: Evaluation.AUTOMATIC,
deleted: false,
});
this.flag = flag;
}
Expand Down
1 change: 1 addition & 0 deletions app/event-management/models/GithubIssueChallenge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export class GithubIssueChallenge extends Challenge {
format: Format.MARKDOWN,
points,
evaluation: Evaluation.MANUAL,
deleted: false,
});
}
}
Expand Down
1 change: 1 addition & 0 deletions app/event-management/models/PhotoChallenge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export class PhotoChallenge extends Challenge {
format,
points,
evaluation: Evaluation.MANUAL,
deleted: false,
});
}
}
Expand Down
1 change: 1 addition & 0 deletions db/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export const challenges = sqliteTable("challenges", {
metadata: text("metadata", { mode: "json" }),
createdAt: text("created_at").default(sql`CURRENT_TIMESTAMP`),
updatedAt: text("updated_at").default(sql`CURRENT_TIMESTAMP`),
deleted: integer("deleted", { mode: "boolean" }).notNull().default(false),
});

export const accessibleChallenges = sqliteTable("accessible_challenges", {
Expand Down
1 change: 1 addition & 0 deletions test/challenges-platform/custom-transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export class CustomChallenge extends Challenge {
format,
points,
evaluation: Evaluation.MANUAL,
deleted: false,
});
this.propString = propString;
this.propNumber = propNumber;
Expand Down
7 changes: 5 additions & 2 deletions test/challenges-platform/services/challenges-service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,12 @@ describe("ChallengesService", () => {

describe("destroy", () => {
it("throws an error", async () => {
const result = await ChallengesService.destroy();
const challenge = await challengeFactory();

expect(result.err).toBe(true);
const result = await ChallengesService.destroy(challenge.uuid);

if (!result.ok) fail("Expected result to be Ok");
expect(result.val.deleted).toBe(true);
});
});

Expand Down
24 changes: 23 additions & 1 deletion test/challenges-platform/services/submissions-service.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { SubmissionService } from "../../../app/challenges-platform";
import {
ChallengesService,
SubmissionService,
} from "../../../app/challenges-platform";
import { challengeFactory } from "../factories/challenge-factory";
import { participantFactory } from "../factories/participant-factory";

Expand Down Expand Up @@ -60,6 +63,25 @@ describe("SubmissionService", () => {
expect(result.val.toString()).toBe("Error: Failed to find challenge");
});
});
describe("when challenge is deleted", () => {
it("returns an error", async () => {
const factory = await challengeFactory();
// Delete the challenge
const challenge = await ChallengesService.destroy(factory.uuid);
if (!challenge.ok)
fail("Expected challenge to be Ok, something went really wrong");

const participant = await participantFactory();

const result = await SubmissionService.create(
challenge.val.uuid,
participant.uuid,
);

expect(result.err).toBe(true);
expect(result.val.toString()).toBe("Error: Challenge is deleted");
});
});
describe("when participant does not exist", () => {
it("returns an error", async () => {
const challenge = await challengeFactory();
Expand Down

0 comments on commit 0f2ab2c

Please sign in to comment.