Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(index.ts): remove CORS config #6

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions src/controllers/application.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { Application } from '../models';
import { NextFunction, Request, Response } from 'express';

const createApplication = async (req: Request, res: Response): Promise<void | Response> => {
const { companyName, position, method, date } = req.body;
if (!companyName || !position || !method || !date) {
const { companyName, position, method, date, employmentType, status } = req.body;
if (!companyName || !position || !method || !date || !employmentType || !status) {
return res.status(400).json({
status: 400,
error: 'Bad Request: Please ensure all required parameters are sent.',
Expand All @@ -15,10 +15,13 @@ const createApplication = async (req: Request, res: Response): Promise<void | Re
position,
method,
date,
status,
employmentType,
user_id: res.locals.user_id,
});
res.status(201).json(application);
} catch (err) {
console.error(err);
res.status(500).json({
status: 500,
error: err,
Expand Down Expand Up @@ -50,13 +53,9 @@ const updateApplication = async (req: Request, res: Response): Promise<void | Re
const getApplications = async (_: Request, res: Response, next: NextFunction): Promise<void | Response> => {
if (!res.locals.user_id) return res.status(403).json({ status: 403, error: 'Bad Request: Please ensure appropriate information submitted.'});
try {
const applications = await Application.findAll({ where: { _id: res.locals.user_id }, raw: true });
const applications = await Application.findAll({ where: { user_id: res.locals.user_id }, raw: true });
res.locals.applications = applications;
applications.length ? next() : res.status(200).json({
applications: [],
offers: [],
interviews: [],
});
applications.length ? next() : res.status(200).json([]);
} catch (err) {
res.status(500).json({ status: 500, error: err });
}
Expand Down
4 changes: 1 addition & 3 deletions src/controllers/offer.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,7 @@ const getOffers = async (_: Request, res: Response): Promise<void | Response> =>
res.locals.applications.forEach(async (app: Application) => {
app.offers = await Offer.findAll<Model<OfferInterface>>({ where: { application_id: app._id}, raw: true });
});
res.status(200).json({
...res.locals.applications,
})
res.status(200).json(res.locals.applications);
} catch (err) {
res.status(500).json({ status: 500, error: err });
}
Expand Down
8 changes: 1 addition & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,7 @@ dotenv.config();
const app = express();

const { PORT } = process.env;

const corsConfig= {
origin: process.env.CORS_ORIGIN,
credentials: true
}

app.use(cors(corsConfig));
app.use(cors());
app.use(express.json());
app.use(router);

Expand Down
2 changes: 2 additions & 0 deletions src/middleware/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const auth = admin.initializeApp({
}).auth();

const authenticate = async (req: Request, res: Response, next: NextFunction): Promise<void | Response> => {
console.log('Someone is making a request!');
const token = req.headers['authorization']?.split(' ')[1];
if (!token) return res.status(403).json({ status: 403, error: 'Incorrect Permissions'});
try {
Expand All @@ -17,6 +18,7 @@ const authenticate = async (req: Request, res: Response, next: NextFunction): Pr
res.locals.user_id = user_id;
next();
} catch (err) {
console.error(err);
res.status(500).json({ status: 500, error: err });
}
}
Expand Down
7 changes: 5 additions & 2 deletions src/models/Application.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ const Application = sequelize.define('application', {
allowNull: false,
defaultValue: uuid(),
},
employmentType: {
type: DataTypes.STRING,
allowNull: false,
},
companyName: {
type: DataTypes.STRING,
allowNull: false,
Expand All @@ -24,10 +28,9 @@ const Application = sequelize.define('application', {
status: {
type: DataTypes.STRING,
allowNull: false,
defaultValue: 'Applied',
},
date: {
type: DataTypes.DATE,
type: DataTypes.STRING,
allowNull: false,
},
});
Expand Down
2 changes: 1 addition & 1 deletion src/models/Offer.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { v4 as uuid } from 'uuid';

const Offer = sequelize.define('offer', {
date: {
type: DataTypes.DATE,
type: DataTypes.STRING,
allowNull: false,
},
_id: {
Expand Down
2 changes: 1 addition & 1 deletion src/models/User.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const User = sequelize.define('user', {
allowNull: true
},
dateStartedLooking: {
type: DataTypes.DATE,
type: DataTypes.STRING,
allowNull: true
},
salaryTarget: {
Expand Down
4 changes: 2 additions & 2 deletions src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ const router = Router();
router.post('/user', authenticate, createUser);
router.put('/user', authenticate, updateUser);
router.post('/applications', authenticate, createApplication);
router.put('/applications', authenticate, updateApplication);
router.put('/applications/:_id', authenticate, updateApplication);
router.post('/interviews', authenticate, createInterview);
router.put('/interviews', authenticate, updateInterview);
router.post('/offers', authenticate, createOffer);
router.put('/offers', authenticate, updateOffer);
router.get('/login', getApplications, getInterviews, getOffers);
router.get('/applications', authenticate, getApplications, getInterviews, getOffers);

export default router;
2 changes: 2 additions & 0 deletions src/types/application.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ export interface Application {
_id: string;
status: string;
position: string;
salary?: number;
user_id: string;
companyName: string;
employmentType: string;
interviews?: Model<InterviewInterface>[];
offers?: Model<OfferInterface>[];
}
1 change: 1 addition & 0 deletions src/types/offer.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ export interface OfferInterface {
salary: string;
benefits: string[];
application_id: string;
status: string;
}