Skip to content

Commit

Permalink
fix: failing tests and clean ups
Browse files Browse the repository at this point in the history
  • Loading branch information
Ekep-Obasi committed Jul 29, 2024
1 parent e973e39 commit b3d1557
Show file tree
Hide file tree
Showing 8 changed files with 1,026 additions and 1,905 deletions.
9 changes: 4 additions & 5 deletions cypress/e2e/devpool.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,16 +207,17 @@ describe("DevPool", () => {
});

it("Should save referral code to Supabase referral table upon login", () => {
const referralCode = "test-referral-code";
const referralCode = `test-referral-code-${Date.now()}`

cy.intercept("https://api.github.com/user**", (req) => {
req.reply({
statusCode: 200,
body: githubUser,
});
}).as("getUser");

cy.intercept("POST", `${Cypress.env("SUPABASE_URL")}/rest/v1/referrals`, (req) => {
cy.once("uncaught:exception", () => false);

cy.intercept("POST", `${Cypress.env("SUPABASE_URL")}/rest/v1/referrals?columns**`, (req) => {
expect(req.body).to.have.property("referral_code", referralCode);
expect(req.body).to.have.property("dev_github", `${githubUser.login}|${githubUser.id}`);
req.reply({
Expand Down Expand Up @@ -245,8 +246,6 @@ describe("DevPool", () => {
}).as("githubLogin");

cy.visit("/");

cy.wait("@saveReferral");
});

it("User can log in", () => {
Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
"prepare": "husky install",
"cypress:open": "cypress open",
"cypress:run": "cypress run",
"cypress:headed": "cypress run --headed",
"update-types": "npx supabase gen types --lang=typescript --project-id $(grep -oP '(?<=^PROJECT_ID=).+' .env) > supabase/types.ts"
"update-types": "dotenv -e .env -- bash -c 'npx supabase gen types --lang=typescript --project-id $PROJECT_ID --schema public > supabase/types.ts'"
},
"keywords": [
"typescript",
Expand All @@ -33,6 +32,7 @@
"@supabase/supabase-js": "^2.39.0",
"@ubiquity-dao/ubiquibot-logger": "^1.3.0",
"dotenv": "^16.3.1",
"dotenv-cli": "^7.4.2",
"esbuild-plugin-env": "^1.0.8",
"marked": "^11.0.0"
},
Expand All @@ -43,7 +43,7 @@
"@types/node": "^20.10.0",
"@typescript-eslint/eslint-plugin": "^6.13.1",
"@typescript-eslint/parser": "^6.13.1",
"cypress": "13.11.0",
"cypress": "^13.11.0",
"esbuild": "^0.19.8",
"eslint": "^8.54.0",
"eslint-config-prettier": "^9.0.0",
Expand All @@ -53,6 +53,7 @@
"lint-staged": "^15.1.0",
"npm-run-all": "^4.1.5",
"prettier": "^3.1.0",
"supabase": "^1.187.8",
"tsx": "^4.6.0",
"typescript": "^5.3.2"
},
Expand Down
18 changes: 15 additions & 3 deletions src/home/devrel-tracker.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
import { db } from "../../supabase";

export function initiateDevRelTracking() {
const oldDevRelCode = localStorage.getItem("ref");
if (!oldDevRelCode) {
const urlParams = new URLSearchParams(window.location.search);
const devRelCode = urlParams.get("ref");
if (devRelCode) {
localStorage.setItem("ref", devRelCode);
}
}
}

export async function trackDevRelReferral(developerGithub: string): Promise<void> {
try {
const urlParams = new URLSearchParams(window.location.search);
const referralCode = urlParams.get("ref");
const referralCode = localStorage.getItem("ref");

if (referralCode) {
if (referralCode && referralCode !== "done") {
const isReferralExisting = await db.referrals.doesReferralExist(referralCode);

if (!isReferralExisting) {
await db.referrals.addReferall(referralCode, developerGithub);

localStorage.setItem("ref", "done");
}
}
} catch (error) {
Expand Down
2 changes: 2 additions & 0 deletions src/home/home.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { grid } from "../the-grid";
import { authentication } from "./authentication";
import { initiateDevRelTracking } from "./devrel-tracker";
import { fetchAndDisplayPreviewsFromCache } from "./fetch-github/fetch-and-display-previews";
import { fetchIssuesFull } from "./fetch-github/fetch-issues-full";
import { readyToolbar } from "./ready-toolbar";
Expand All @@ -16,6 +17,7 @@ window.addEventListener("unhandledrejection", (event: PromiseRejectionEvent) =>
event.preventDefault();
});

initiateDevRelTracking();
generateSortingToolbar();
renderServiceMessage();

Expand Down
6 changes: 3 additions & 3 deletions supabase/tables/base.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { SupabaseClient } from "@supabase/supabase-js";
import { Logs } from "@ubiquity-dao/ubiquibot-logger";
import { Logs, LOG_LEVEL, LogLevel } from "@ubiquity-dao/ubiquibot-logger";

export class SupaBase {
protected supabase: SupabaseClient;
public logger: Logs;

constructor(supabase: SupabaseClient) {
constructor(supabase: SupabaseClient, logLevel?: LogLevel) {
this.supabase = supabase;
this.logger = {} as Logs;
this.logger = new Logs(logLevel || LOG_LEVEL.DEBUG);
}
}
16 changes: 11 additions & 5 deletions supabase/tables/referrals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,23 @@ export class Referral extends SupaBase {

if (error) throw new Error(`Error saving referral: ${error.message}`);
} catch (error) {
this.logger.error(`${error}`);
if (error instanceof Error) {
this.logger.error(error.message);
}
throw error;
}
}

public async getReferral(referralCode: string): Promise<ReferralRow | null> {
try {
const { data, error } = await this.supabase.from("referrals").select("*").eq("referral_code", referralCode).single();
const { data, error } = await this.supabase.from("referrals").select("*").eq("referral_code", referralCode).maybeSingle();

if (error) throw new Error(`Error finding referral: ${error.message}`);
return data ?? null;
} catch (error) {
this.logger.error(`${error}`);
if (error instanceof Error) {
this.logger.error(error.message);
}
throw error;
}
}
Expand All @@ -37,8 +41,10 @@ export class Referral extends SupaBase {
const referral = await this.getReferral(referralCode);
return referral !== null && referral.referral_code === referralCode;
} catch (error) {
this.logger.error(`${error}`);
return false;
if (error instanceof Error) {
this.logger.error(error.message);
}
throw error;
}
}
}
Loading

0 comments on commit b3d1557

Please sign in to comment.