Skip to content

Commit

Permalink
Merge pull request #60 from ChangePlusPlusVandy/fake-data-update
Browse files Browse the repository at this point in the history
Fake data update
  • Loading branch information
JiashuHarryHuang authored Nov 11, 2023
2 parents eabee8a + eced98d commit 2a77b40
Show file tree
Hide file tree
Showing 12 changed files with 367 additions and 237 deletions.
4 changes: 2 additions & 2 deletions components/Event/Event.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { QueriedVolunteerEventData } from 'bookem-shared/src/types/database';
import { QueriedVolunteerEventDTO } from 'bookem-shared/src/types/database';
import React, { useState } from 'react';
import Header from '@/components/Event/Header';
import BookIcon from '@/components/Event/BookIcon';
Expand All @@ -21,7 +21,7 @@ import { BOOKEM_THEME } from '@/utils/constants';
* Event Detail
* @param event Data about the event
*/
const Event = ({ event }: { event: QueriedVolunteerEventData }) => {
const Event = ({ event }: { event: QueriedVolunteerEventDTO }) => {
/**
* True: display About
* False: display Contact
Expand Down
10 changes: 5 additions & 5 deletions components/Event/EventName.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { QueriedVolunteerEventData } from 'bookem-shared/src/types/database';
import { QueriedVolunteerEventDTO } from 'bookem-shared/src/types/database';
import React, { useEffect } from 'react';
import {
EventNameBox,
Expand All @@ -15,7 +15,7 @@ import Image from 'next/image';
* Calculate the length of the event volunteers
* If event.volunteers is undefined, return 0
*/
export const getEventLength = (event: QueriedVolunteerEventData) => {
export const getEventLength = (event: QueriedVolunteerEventDTO) => {
if (event.volunteers && event.volunteers.length)
return event.volunteers.length;
else return 0;
Expand All @@ -33,7 +33,7 @@ const EventName = ({
}: {
signedUp: boolean;
setSignedUp: (signedUp: boolean) => void;
event: QueriedVolunteerEventData;
event: QueriedVolunteerEventDTO;
signUpEvent: () => void;
}) => {
const { data: session } = useSession();
Expand All @@ -54,7 +54,7 @@ const EventName = ({
<Media greaterThanOrEqual="sm">
<NameAndSpot>
<b>{event.name}</b>
{event.tags.length > 0 && <p>({event.program.tagName})</p>}
{event.tags.length > 0 && <p>({event.program?.name})</p>}
<br />
{getEventLength(event)}/{event.maxSpot} spots filled
</NameAndSpot>
Expand All @@ -66,7 +66,7 @@ const EventName = ({
{/* Mobile */}
<Media lessThan="sm">
<NameAndSpot>
<b>{event.name}</b> <span>({event.program?.tagName}) </span>
<b>{event.name}</b> <span>({event.program?.name}) </span>
<br />
<StatusBox>
<Image src="/event/dot.svg" alt="" width={10} height={10} />
Expand Down
4 changes: 2 additions & 2 deletions components/Event/Footer.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { SignupButton } from '@/styles/components/Event/event.styles';
import { QueriedVolunteerEventData } from 'bookem-shared/src/types/database';
import { QueriedVolunteerEventDTO } from 'bookem-shared/src/types/database';
import { useSession } from 'next-auth/react';
import React, { useEffect } from 'react';
import { getEventLength } from '@/components/Event/EventName';
Expand All @@ -20,7 +20,7 @@ const Footer = ({
}: {
signedUp: boolean;
setSignedUp: (signedUp: boolean) => void;
event: QueriedVolunteerEventData;
event: QueriedVolunteerEventDTO;
signUpEvent: () => void;
}) => {
// Get user id in session
Expand Down
5 changes: 1 addition & 4 deletions components/Home/PastActivity.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@ const dummyEventData: QueriedVolunteerEventData = {
},
phone: '123-456-7890',
email: '[email protected]',
program: {
_id: new mongoose.Types.ObjectId(),
tagName: 'BNFK',
},
program: new mongoose.Types.ObjectId(),
requireApplication: true,
volunteers: [],
tags: [],
Expand Down
390 changes: 197 additions & 193 deletions package-lock.json

Large diffs are not rendered by default.

51 changes: 51 additions & 0 deletions pages/api/scripts/02-insert-tags-programs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import dbConnect from '@/lib/dbConnect';
import { NextApiRequest, NextApiResponse } from 'next';
import Tags from 'bookem-shared/src/models/Tags';
import VolunteerPrograms from 'bookem-shared/src/models/VolunteerPrograms';
import { INSERTED_PROGRAMS, INSERTED_TAGS } from './constants';
import { generateProgram, generateTag } from './helper-functions';

export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
switch (req.method) {
case 'GET':
try {
// Connect to the database
await dbConnect();

// TODO:
// delete all tags
await Tags.deleteMany({});

// delete all programs
await VolunteerPrograms.deleteMany({});

// Generate programs
INSERTED_PROGRAMS.forEach(async program => {
const generatedProgram = generateProgram(program);
await VolunteerPrograms.insertMany(generatedProgram);
});

// Generate tags
INSERTED_TAGS.forEach(async tag => {
const genratedTag = generateTag(tag);
await Tags.insertMany(genratedTag);
});

res.status(200).json({
success: true,
message: 'Inserted fake tags and program data',
});
} catch (error: any) {
res.status(500).json({ success: false, error: error.message });
}

break;

default:
res.status(400).json({ success: false });
break;
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
import dbConnect from '@/lib/dbConnect';
import VolunteerEvents from 'bookem-shared/src/models/VolunteerEvents';
import { NextApiRequest, NextApiResponse } from 'next';
import { generateEvent } from '@/pages/api/scripts/helper-functions';
import { EventStatus } from '@/pages/api/scripts/constants';
import {
fillProgramEvents,
generateEvent,
generateProgram,
generateTag,
} from '@/pages/api/scripts/helper-functions';
import {
INSERTED_PROGRAMS,
INSERTED_TAGS,
} from '@/pages/api/scripts/constants';
import Tags from 'bookem-shared/src/models/Tags';
import VolunteerPrograms from 'bookem-shared/src/models/VolunteerPrograms';
import {
QueriedTagData,
QueriedVolunteerEventData,
QueriedVolunteerProgramData,
} from 'bookem-shared/src/types/database';

export default async function handler(
req: NextApiRequest,
Expand All @@ -17,19 +32,34 @@ export default async function handler(
// delete all events
await VolunteerEvents.deleteMany({});

// get all tags
const tags = await Tags.find({});

// get all programs
const programs = await VolunteerPrograms.find({});

// create a bulk operation to minimize the number of db calls
const bulkEvents =
VolunteerEvents.collection.initializeUnorderedBulkOp();

// insert a bunch of equally distributed events
for (let i = 0; i < 100; i++) {
const event = generateEvent(i);
const event = generateEvent(
i,
tags as QueriedTagData[],
programs as QueriedVolunteerProgramData[]
);
bulkEvents.insert(event);
}

// execute the bulk operation
await bulkEvents.execute();

// Query them back to update program
const events = await VolunteerEvents.find({ program: { $ne: null } });
// Update programs so that programs contain their corresponding events
await fillProgramEvents(events);

res.status(200).json({
success: true,
message:
Expand Down
44 changes: 25 additions & 19 deletions pages/api/scripts/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,80 +8,86 @@ export const GENDERS = ['male', 'female'];
// ------------------ CONFIGURATIONS ------------------
export const EVENTS: {
name: string;
tag: string;
program: string;
isMultipleDays: boolean;
requireApplication: boolean;
tags?: string[];
}[] = [
{
name: 'Distribute books',
tag: 'BFNK',
program: 'BFNK',
isMultipleDays: false,
requireApplication: false,
},
{
name: 'Reading role model',
tag: 'RFR',
program: 'RFR',
isMultipleDays: true,
requireApplication: true,
},
{
name: 'Book drive',
tag: '',
program: '',
isMultipleDays: false,
requireApplication: false,
},
{
name: 'Special event',
tag: '',
program: '',
isMultipleDays: false,
requireApplication: false,
},
{
name: 'Interactive reading',
tag: 'RIF',
program: 'RIF',
isMultipleDays: true,
requireApplication: true,
},
{
name: 'Book sort, clean, process',
tag: '',
program: '',
isMultipleDays: false,
requireApplication: false,
},
{
name: 'Office work',
tag: '',
program: '',
tags: ['saved'],
isMultipleDays: false,
requireApplication: false,
},
{
name: 'Book bus',
tag: '',
program: '',
tags: ['hidden'],
isMultipleDays: false,
requireApplication: false,
},
];

export const INSERTED_TAGS = [
{
_id: new mongoose.Types.ObjectId('642a4dec74c697623278344d'),
tagName: 'RIF',
_id: new mongoose.Types.ObjectId('642a4e0a74c6976232783450'),
tagName: 'saved',
},
{
_id: new mongoose.Types.ObjectId('642a4dfc74c697623278344e'),
tagName: 'RFR',
_id: new mongoose.Types.ObjectId('642a4e1274c6976232783451'),
tagName: 'hidden',
},
];

export const INSERTED_PROGRAMS = [
{
_id: new mongoose.Types.ObjectId('642a4e0474c697623278344f'),
tagName: 'BFNK',
_id: new mongoose.Types.ObjectId('642a4dec74c697623278344d'),
name: 'RIF',
},
{
_id: new mongoose.Types.ObjectId('642a4e0a74c6976232783450'),
tagName: 'saved',
_id: new mongoose.Types.ObjectId('642a4dfc74c697623278344e'),
name: 'RFR',
},
{
_id: new mongoose.Types.ObjectId('642a4e1274c6976232783451'),
tagName: 'hidden',
_id: new mongoose.Types.ObjectId('642a4e0474c697623278344f'),
name: 'BFNK',
},
];

Expand Down
Loading

1 comment on commit 2a77b40

@vercel
Copy link

@vercel vercel bot commented on 2a77b40 Nov 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

bookem-user – ./

bookem-user.vercel.app
bookem-user-bookem-user.vercel.app
bookem-user-git-main-bookem-user.vercel.app

Please sign in to comment.