Skip to content

Commit

Permalink
Merge pull request #142 from UoaWDCC/fix-background-color
Browse files Browse the repository at this point in the history
fix: checker page background color
  • Loading branch information
alexwillmcleod authored Oct 18, 2023
2 parents cb2a0d1 + 18d8b88 commit 2a73b73
Show file tree
Hide file tree
Showing 8 changed files with 163 additions and 169 deletions.
104 changes: 44 additions & 60 deletions api/routes/pages/pages.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,29 @@
import express, { Request, Response } from 'express';
import { PrismaClient, User } from '@prisma/client';
import { nanoid } from 'nanoid';
import auth from '../../middleware/auth';
import { JWT } from 'google-auth-library';
import { drive_v3, google } from 'googleapis';
import multer, { memoryStorage } from 'multer';
import { supabase } from '../..';
import { v4 as uuidv4 } from 'uuid';
import IPageCustomization from '../types/IPageCustomization';
import serviceClient from '../../service';
import express, { Request, Response } from "express";
import { PrismaClient, User } from "@prisma/client";
import { nanoid } from "nanoid";
import auth from "../../middleware/auth";
import { JWT } from "google-auth-library";
import { drive_v3, google } from "googleapis";
import multer, { memoryStorage } from "multer";
import { supabase } from "../..";
import { v4 as uuidv4 } from "uuid";
import IPageCustomization from "../types/IPageCustomization";
import serviceClient from "../../service";

const prisma = new PrismaClient();
export const router = express.Router();

const storage = memoryStorage();
const upload = multer({ storage });

interface PageCustomization {
name: string;
organisationId: string;
sheetId: string;
sheetTabId: string;
backgroundColor?: string;
textFieldBackgroundColor?: string;
textColor?: string;
buttonColor?: string;
headingColor?: string;
logoLink?: string;
backgroundImageLink?: string;
fontFamily?: string;
identificatonColumns: {
originalName: string;
displayName?: string;
}[];
}
router.post(
'/create',
"/create",
upload.fields([
{
name: 'background',
name: "background",
},
{
name: 'logo',
name: "logo",
},
]),
auth,
Expand Down Expand Up @@ -69,7 +51,7 @@ router.post(
return res
.status(400)
.send(
'`name`, `organisationId`, `sheetId`, `sheetTabId`, and `identificationColumns` are required fields'
"`name`, `organisationId`, `sheetId`, `sheetTabId`, and `identificationColumns` are required fields"
);

// Check if the sheet id is already in use
Expand All @@ -88,13 +70,13 @@ router.post(

try {
const drive = google.drive({
version: 'v3',
version: "v3",
auth: serviceClient,
});
console.log(await drive.files.list());
const permissions = await drive.permissions.list({
fileId: sheetId,
fields: 'permissions(emailAddress)',
fields: "permissions(emailAddress)",
});
const isSharedWithEmail = permissions.data.permissions!.some(
(permission: drive_v3.Schema$Permission) =>
Expand All @@ -103,13 +85,13 @@ router.post(
if (!isSharedWithEmail)
return res
.status(401)
.send('unauthorised to access this spreadsheet');
.send("unauthorised to access this spreadsheet");
} catch (err) {
console.error(err);
return res
.status(500)
.send(
'failed to check if sheet is shared with user. make sure this spreadsheet is shared with the service account.'
"failed to check if sheet is shared with user. make sure this spreadsheet is shared with the service account."
);
}

Expand All @@ -124,15 +106,15 @@ router.post(
const fileName = background[0].originalname;
const buffer = background[0].buffer;
const { data, error } = await supabase.storage
.from('image-bucket')
.upload(`${uuidv4()}.${fileName.split('.').pop()}`, buffer, {
contentType: 'image/*',
.from("image-bucket")
.upload(`${uuidv4()}.${fileName.split(".").pop()}`, buffer, {
contentType: "image/*",
});
if (error) {
console.error(`Error: ${JSON.stringify(error)}`);
return res
.status(500)
.send('failed to upload background to storage bucket');
.send("failed to upload background to storage bucket");
}
backgroundUrl = storageBucketUrlPrefix + data.path;
}
Expand All @@ -141,15 +123,15 @@ router.post(
const fileName = logo[0].originalname;
const buffer = logo[0].buffer;
const { data, error } = await supabase.storage
.from('image-bucket')
.upload(`${uuidv4()}.${fileName.split('.').pop()}`, buffer, {
contentType: 'image/*',
.from("image-bucket")
.upload(`${uuidv4()}.${fileName.split(".").pop()}`, buffer, {
contentType: "image/*",
});
if (error) {
console.error(`Error: ${JSON.stringify(error)}`);
return res
.status(500)
.send('failed to upload logo to storage bucket');
.send("failed to upload logo to storage bucket");
}
logoUrl = storageBucketUrlPrefix + data.path;
}
Expand All @@ -170,9 +152,10 @@ router.post(
textColor: rest.textColor,
buttonColor: rest.buttonColor,
headingColor: rest.headingColor,
dropDownBackgroundColor: rest.dropDownBackgroundColor,
logoLink: logoUrl,
backgroundImageLink: backgroundUrl,
fontFamily: fontFamily || 'Montserrat',
fontFamily: fontFamily || "Montserrat",
},
});

Expand All @@ -192,19 +175,19 @@ router.post(
res.status(200).json({ pathId });
} catch (error) {
console.error(`Error creating page: ${error}`);
res.status(400).json({ error: 'Error creating page' });
res.status(400).json({ error: "Error creating page" });
}
}
);

router.get(
'/verify/:webLink/:columnName/:value',
"/verify/:webLink/:columnName/:value",
async (req: Request, res: Response) => {
const { webLink, columnName, value } = req.params;
if (!webLink || !columnName || !value)
return res
.status(400)
.send('`pageId`, `columnName`, and `value` are required fields');
.send("`pageId`, `columnName`, and `value` are required fields");

const page = await prisma.page.findFirst({
where: {
Expand All @@ -216,7 +199,7 @@ router.get(
return res.status(400).send(`could not find page with link ${webLink}`);

// intialise credentials
const sheets = google.sheets({ version: 'v4', auth: serviceClient });
const sheets = google.sheets({ version: "v4", auth: serviceClient });
const columnData: {
[key: string]: { id: string; name: string; unique: boolean };
} = {};
Expand All @@ -233,7 +216,7 @@ router.get(
if (!metadataResponse.data.sheets) {
return res
.status(400)
.send(JSON.stringify('spreadsheet has no sheets'));
.send(JSON.stringify("spreadsheet has no sheets"));
}

// Find the sheet ID that matches the given gid
Expand All @@ -243,7 +226,7 @@ router.get(

// If the sheet ID is not found, return an error
if (!sheet) {
return res.status(404).send(JSON.stringify('sheet not found'));
return res.status(404).send(JSON.stringify("sheet not found"));
}

// Get the name of the sheet
Expand All @@ -263,7 +246,7 @@ router.get(
range,
});

if (!values) return res.status(500).send('club has no members');
if (!values) return res.status(500).send("club has no members");

const originalColumn = await prisma.column.findFirst({
where: {
Expand Down Expand Up @@ -300,7 +283,7 @@ router.get(
try {
return element.toLowerCase();
} catch (err) {
return '';
return "";
}
})
.includes(value.toLowerCase())
Expand All @@ -314,7 +297,7 @@ router.get(
},
});

return res.status(200).send('value found in column');
return res.status(200).send("value found in column");
} else {
await prisma.membershipCheckUsage.create({
data: {
Expand All @@ -325,16 +308,16 @@ router.get(
},
});

return res.status(200).send('could not find user in column');
return res.status(200).send("could not find user in column");
}
} catch (err) {
console.error(err);
return res.status(500).send('error retrieving data');
return res.status(500).send("error retrieving data");
}
}
);

router.get('/info/:webLink', async (req: Request, res: Response) => {
router.get("/info/:webLink", async (req: Request, res: Response) => {
const { webLink } = req.params;

const pageData = await prisma.page.findFirst({
Expand All @@ -344,7 +327,7 @@ router.get('/info/:webLink', async (req: Request, res: Response) => {
});

if (!pageData)
return res.status(400).send('failed to get data with that link');
return res.status(400).send("failed to get data with that link");

const columnData = await prisma.column.findMany({
where: {
Expand All @@ -353,11 +336,12 @@ router.get('/info/:webLink', async (req: Request, res: Response) => {
});

if (!columnData)
return res.status(400).send('failed to get columns data with that link');
return res.status(400).send("failed to get columns data with that link");

const dataToReturn: any = {
title: pageData?.name,
backgroundColor: pageData?.backgroundColor,
dropDownBackgroundColor: pageData.dropDownBackgroundColor,
textFieldBackgroundColor: pageData?.textFieldBackgroundColor,
textFieldtextColor: pageData?.textColor,
buttonColor: pageData?.buttonColor,
Expand Down
1 change: 1 addition & 0 deletions api/routes/types/IPageCustomization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export default interface IPageCustomization {
sheetTabId: string;
backgroundColor?: string;
textFieldBackgroundColor?: string;
dropDownBackgroundColor: string;
textColor?: string;
buttonColor?: string;
headingColor?: string;
Expand Down
Loading

0 comments on commit 2a73b73

Please sign in to comment.