-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
user signup/login teams fixtures, alot
- Loading branch information
Victor
committed
Aug 1, 2022
1 parent
0f73fd7
commit ffd0f41
Showing
28 changed files
with
1,300 additions
and
145 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { Express } from 'express'; | ||
|
||
import { User } from '../../src/user/interfaces'; | ||
|
||
declare global { | ||
namespace Express { | ||
interface Request { | ||
user: User; | ||
} | ||
} | ||
} | ||
|
||
// export interface CustomRequest extends Request { | ||
// user?: User; | ||
// } | ||
|
||
// import { User } from '../src/user/interfaces'; | ||
|
||
// declare module 'express' { | ||
// export interface Request { | ||
// user?: User; | ||
// } | ||
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { NextFunction, Request, Response } from 'express'; | ||
import { clone } from 'lodash'; | ||
|
||
import logger from '../../logger'; | ||
import catchAsync from '../../utils/catchAsync'; | ||
import { signToken } from '../../utils/signToken'; | ||
import { Team } from '../interfaces'; | ||
import { create, findOne, update } from '../services/fixturesService'; | ||
|
||
export const createTeam = catchAsync( | ||
async (req: Request, res: Response, next: NextFunction): Promise<void> => { | ||
try { | ||
const { name, shortName, email }: Team = req.body; | ||
const user = clone(req.user); | ||
const payload = { | ||
name, | ||
shortName, | ||
email, | ||
createdBy: user?.id, | ||
} as Team; | ||
|
||
const data = await create(payload); | ||
|
||
res.status(201).json({ | ||
message: 'Team created successfully', | ||
status: 'success', | ||
data, | ||
}); | ||
} catch (error: any) { | ||
logger.error( | ||
`Error occurred while creating team: ${JSON.stringify(error)}` | ||
); | ||
res.status(error.statusCode || 500).json({ | ||
status: error.status || 'error', | ||
message: error.message, | ||
}); | ||
} | ||
} | ||
); | ||
|
||
export const findTeamById = catchAsync( | ||
async (req: Request, res: Response, next: NextFunction): Promise<void> => { | ||
try { | ||
const { id } = req.params; | ||
|
||
const data = await findOne(id); | ||
|
||
res.status(200).json({ | ||
message: 'Team fetched successfully', | ||
status: 'success', | ||
data, | ||
}); | ||
} catch (error: any) { | ||
logger.error( | ||
`Error occurred while fetching team: ${JSON.stringify(error)}` | ||
); | ||
res.status(error.statusCode || 500).json({ | ||
status: error.status || 'error', | ||
message: error.message, | ||
}); | ||
} | ||
} | ||
); | ||
|
||
export const updateTeam = catchAsync( | ||
async (req: Request, res: Response, next: NextFunction): Promise<void> => { | ||
try { | ||
const { id } = req.params; | ||
|
||
const payload = { | ||
...req.body, | ||
} as Omit<Team, 'updatedAt'>; | ||
|
||
const data = await update(id, payload); | ||
|
||
res.status(200).json({ | ||
message: 'Team Updated successfully', | ||
status: 'success', | ||
data, | ||
}); | ||
} catch (error: any) { | ||
logger.error( | ||
`Error occurred while fetching team: ${JSON.stringify(error)}` | ||
); | ||
res.status(error.statusCode || 500).json({ | ||
status: error.status || 'error', | ||
message: error.message, | ||
}); | ||
} | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { Document } from 'mongoose'; | ||
|
||
export interface Fixture extends Document { | ||
name: string; | ||
shortName: string; | ||
email: string; | ||
readonly createdBy: string; | ||
readonly createdAt?: Date; | ||
updatedAt?: Date; | ||
} | ||
|
||
export enum FixtureStatues { | ||
Completed = 'Completed', | ||
Pending = 'Pending', | ||
Ongoing = 'Ongoing', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import mongoose, { Schema, HookNextFunction } from 'mongoose'; | ||
import * as bcrypt from 'bcryptjs'; | ||
import validator from 'validator'; | ||
|
||
import { Fixture, FixtureStatues } from '../interfaces'; | ||
import moment from 'moment'; | ||
|
||
const fixtureSchema = new Schema( | ||
{ | ||
home: { | ||
team: { | ||
type: Schema.Types.ObjectId, | ||
ref: 'Team', | ||
required: true, | ||
}, | ||
score: { | ||
type: Number, | ||
default: 0, | ||
}, | ||
}, | ||
away: { | ||
team: { | ||
type: Schema.Types.ObjectId, | ||
ref: 'Team', | ||
required: true, | ||
}, | ||
score: { | ||
type: Number, | ||
default: 0, | ||
}, | ||
}, | ||
status: { | ||
type: String, | ||
enum: { | ||
values: FixtureStatues, | ||
message: 'Team status is either completed, pending or ongoing', | ||
}, | ||
default: FixtureStatues.Pending, | ||
}, | ||
createdBy: { | ||
type: Schema.Types.ObjectId, | ||
ref: 'User', | ||
required: true, | ||
}, | ||
createdAt: { | ||
type: Date, | ||
default: Date.now(), | ||
}, | ||
updatedAt: { | ||
type: Date, | ||
}, | ||
}, | ||
{ | ||
toJSON: { virtuals: true }, | ||
toObject: { virtuals: true }, | ||
} | ||
); | ||
|
||
fixtureSchema.pre('save', function (next: HookNextFunction) { | ||
const fixture = this as Fixture; | ||
|
||
if (fixture.isNew) return next(); | ||
|
||
fixture.updatedAt = moment().toDate(); | ||
|
||
next(); | ||
}); | ||
|
||
const Team = mongoose.model<Fixture>('Fixture', fixtureSchema); | ||
|
||
export default Team; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { NextFunction, Request, Response } from 'express'; | ||
|
||
import logger from '../../logger'; | ||
import AppError from '../../utils/appError'; | ||
import { Fixture } from '../interfaces'; | ||
import FixtureModel from '../models/fixturesModel'; | ||
|
||
export const create = async (payload: Fixture): Promise<Fixture> => { | ||
logger.info(`Creating fixture with payload', ${JSON.stringify(payload)}`); | ||
|
||
return FixtureModel.create(payload); | ||
}; | ||
|
||
export const findOne = async (id: string): Promise<Fixture> => { | ||
logger.info(`finding fixture with ID ${id}`); | ||
|
||
const fixture = (await FixtureModel.findById(id) | ||
.populate({ | ||
path: 'createdBy', | ||
select: 'name role', | ||
}) | ||
.populate({ | ||
path: 'home', | ||
select: 'name shortName', | ||
}) | ||
.populate({ | ||
path: 'away', | ||
select: 'name shortName', | ||
})) as Fixture; | ||
|
||
if (!fixture) { | ||
throw new AppError('No fixture found with that ID', 400); | ||
} | ||
|
||
return fixture; | ||
}; | ||
|
||
export const update = async ( | ||
id: string, | ||
payload: Fixture | ||
): Promise<Fixture> => { | ||
logger.info( | ||
`attempting to update team with ID: ${id} with paylod: ${JSON.stringify( | ||
payload | ||
)}` | ||
); | ||
|
||
const fixture = (await FixtureModel.findOneAndUpdate({ _id: id }, payload, { | ||
new: true, | ||
})) as Fixture; | ||
|
||
if (!fixture) { | ||
throw new AppError('No fixture found with that ID', 400); | ||
} | ||
|
||
return fixture; | ||
}; |
Oops, something went wrong.