Skip to content

Commit

Permalink
implement GET and MODIFY for hacker document
Browse files Browse the repository at this point in the history
Co-authored-by: Gabriel Dong <gabrielhdong@users.noreply.github.com>
  • Loading branch information
zineanteoh and Gabriel Dong committed Sep 9, 2023
1 parent 17f29cf commit 0199e21
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 15 deletions.
31 changes: 23 additions & 8 deletions scripts/cli-util/get-hacker.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
import { input, select } from '@inquirer/prompts';
import { UserData } from '../../types/database';
import useSWR from 'swr';
import dbConnect from '../../middleware/database';
import User from '../../models/user';
import team from '../../models/team';
import { promptAction } from '../dev-cli';
import { UserData, TeamData } from '../../types/database';

export const handleGetHacker = async () => {
const hackerEmail = await input({
message: 'Enter hacker email',
});

const user: UserData | null = JSON.parse(JSON.stringify(await User.findOne({ email: hackerEmail })));
if (!user) {
console.log('Hacker not found');
return promptAction();
}

const subAction1 = await select({
message: 'Select an action to perform',
choices: [
{
name: 'Get events',
name: 'Get events attended',
value: 'get-events',
},
{
Expand All @@ -30,18 +38,21 @@ export const handleGetHacker = async () => {
],
});

// connect to db
await dbConnect();

switch (subAction1) {
case 'get-events':
await getEvents();
break;
case 'get-team':
await getTeam();
await getTeam(user);
break;
case 'get-application':
await getApplication();
break;
case 'get-document':
await getDocument();
console.log(user);
break;
default:
console.log('Invalid action');
Expand All @@ -50,8 +61,12 @@ export const handleGetHacker = async () => {

const getEvents = async () => {};

const getTeam = async () => {};
const getTeam = async (user: UserData) => {
const teamId = user.team;
const hackerTeam = JSON.parse(JSON.stringify(await team.findOne({ _id: teamId })));
console.log(hackerTeam);

const getApplication = async () => {};
return promptAction();
};

const getDocument = async () => {};
const getApplication = async () => {};
29 changes: 22 additions & 7 deletions scripts/cli-util/modify-hacker.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { input, select } from '@inquirer/prompts';
import dbConnect from '../../middleware/database';
import User from '../../models/user';
import { promptAction } from '../dev-cli';
import { ApplicationStatus, UserData } from '../../types/database';

// TODO: zi
/**
Expand All @@ -12,8 +14,17 @@ export const handleModifyHacker = async () => {
message: 'Enter hacker email',
});

// query for hacker document
const hacker: UserData | null = await User.findOne({ email: hackerEmail });

// if hacker not found, re-prompt
if (!hacker) {
console.log('Oops, Hacker not found! Please try again.');
return promptAction();
}

// get sub-action
const subAction1 = await select({
const subAction = await select({
message: 'Select sub-action to perform',
choices: [
{
Expand All @@ -39,9 +50,10 @@ export const handleModifyHacker = async () => {
],
});

// perf
switch (subAction1) {
// perform sub-action
switch (subAction) {
case 'change-status':
await changeStatus(hacker);
break;
case 'delete-application':
break;
Expand All @@ -54,7 +66,11 @@ export const handleModifyHacker = async () => {
}
};

const changeStatus = async () => {
const changeStatus = async (hacker: UserData) => {
const oldStatus: ApplicationStatus = hacker.applicationStatus;

// console.log(`Current status: ${Object.values(ApplicationStatus)[oldStatus]}`);

const newStatus = await select({
message: 'Select new status',
choices: [
Expand Down Expand Up @@ -89,8 +105,7 @@ const changeStatus = async () => {
],
});

//
await dbConnect();
// TODO:
};

const deleteApplication = async () => {};
Expand Down
11 changes: 11 additions & 0 deletions scripts/dev-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ const executeCLI = async () => {
],
});

// connect to db
await dbConnect(process.env.DATABASE_URL);

// prompt action
await promptAction();

// exit
process.exit(0);
};

export const promptAction = async () => {
// TODO:
// list actions we as devs will need to do and can be simplified with this CLI tool
// [ ] populate collections with dummy data
Expand Down

0 comments on commit 0199e21

Please sign in to comment.