-
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.
- Loading branch information
Showing
4 changed files
with
141 additions
and
0 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,66 @@ | ||
import { withIronSessionApiRoute } from 'iron-session/next'; | ||
import { NextApiRequest, NextApiResponse } from 'next'; | ||
import { IMember, Member, MemberDocument } from '../../../src/models/Member'; | ||
import { ResponseData } from '../../../src/types/index'; | ||
import { ironOptions } from '../../../src/util/ironConfig'; | ||
import { isValidObjectId } from 'mongoose'; | ||
import { StatusCodes, getReasonPhrase } from 'http-status-codes'; | ||
import { Authentication } from '../../../src/auth/auth'; | ||
import { AuthorityLevel } from '../../../src/models/Admin'; | ||
|
||
export default withIronSessionApiRoute(async function createMember( | ||
req: Omit<NextApiRequest, 'body'> & { body: IMember; } & { query: { member_id?: string; }; }, | ||
res: NextApiResponse<ResponseData<MemberDocument | MemberDocument[]>>, | ||
): Promise<void> { | ||
switch (req.method) { | ||
case 'GET': { | ||
if (!isValidObjectId(req.query.member_id)) { | ||
res.status(StatusCodes.BAD_REQUEST).json({ | ||
error: true, | ||
message: getReasonPhrase(StatusCodes.BAD_REQUEST), | ||
data: null, | ||
}); | ||
|
||
return; | ||
} | ||
|
||
const member = await Member.findById(req.query.member_id); | ||
|
||
if (!member) { | ||
res.status(StatusCodes.NOT_FOUND).json({ | ||
error: true, | ||
message: getReasonPhrase(StatusCodes.NOT_FOUND), | ||
data: null, | ||
}); | ||
|
||
return; | ||
} | ||
|
||
res.status(StatusCodes.OK).json({ | ||
error: false, | ||
message: getReasonPhrase(StatusCodes.OK), | ||
data: member, | ||
}); | ||
} break; | ||
|
||
case 'POST': { | ||
if (!Authentication.authenticate(AuthorityLevel.ADMIN, req)) { | ||
res.status(StatusCodes.FORBIDDEN).json({ | ||
error: true, | ||
message: getReasonPhrase(StatusCodes.FORBIDDEN), | ||
data: null, | ||
}); | ||
|
||
return; | ||
} | ||
|
||
const member = await Member.create(req.body); | ||
|
||
res.status(StatusCodes.CREATED).json({ | ||
error: false, | ||
message: getReasonPhrase(StatusCodes.CREATED), | ||
data: member, | ||
}); | ||
} break; | ||
} | ||
}, ironOptions); |
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,47 @@ | ||
import { withIronSessionApiRoute } from 'iron-session/next'; | ||
import { NextApiRequest, NextApiResponse } from 'next'; | ||
import { IMember, Member, MemberDocument } from '../../../src/models/Member'; | ||
import { ResponseData } from '../../../src/types/index'; | ||
import { ironOptions } from '../../../src/util/ironConfig'; | ||
import { StatusCodes, getReasonPhrase } from 'http-status-codes'; | ||
import { Authentication } from '../../../src/auth/auth'; | ||
import { AuthorityLevel } from '../../../src/models/Admin'; | ||
|
||
export default withIronSessionApiRoute(async function createMember( | ||
req: Omit<NextApiRequest, 'body'> & { body: IMember; } & { query: { member_id?: string; }; }, | ||
res: NextApiResponse<ResponseData<MemberDocument | MemberDocument[]>>, | ||
): Promise<void> { | ||
switch (req.method) { | ||
case 'POST': { | ||
if (!Authentication.authenticate(AuthorityLevel.ADMIN, req)) { | ||
res.status(StatusCodes.FORBIDDEN).json({ | ||
error: true, | ||
message: getReasonPhrase(StatusCodes.FORBIDDEN), | ||
data: null, | ||
}); | ||
|
||
return; | ||
} | ||
|
||
const member = new Member(req.body); | ||
|
||
try { | ||
await member.save(); | ||
|
||
res.status(StatusCodes.CREATED).json({ | ||
error: false, | ||
message: getReasonPhrase(StatusCodes.CREATED), | ||
data: member, | ||
}); | ||
} catch (e) { | ||
console.error(e); | ||
|
||
res.status(StatusCodes.BAD_REQUEST).json({ | ||
error: true, | ||
message: getReasonPhrase(StatusCodes.BAD_REQUEST), | ||
data: null, | ||
}); | ||
} | ||
} break; | ||
} | ||
}, ironOptions); |
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,27 @@ | ||
import { default as mongoose, HydratedDocument, Model, Schema } from 'mongoose'; | ||
|
||
export interface IMember { | ||
name: string; | ||
email: string; | ||
paymentMethod: string; | ||
memberNumber: string; | ||
idc: string; | ||
indeks: string; | ||
} | ||
|
||
export type MemberDocument = HydratedDocument<IMember>; | ||
|
||
export type MemberModel = Model<IMember>; | ||
|
||
export const memberSchema = new Schema<IMember, MemberModel>({ | ||
name: { type: Schema.Types.String, required: true }, | ||
email: { type: Schema.Types.String, required: true }, | ||
paymentMethod: { type: Schema.Types.String, required: true }, | ||
memberNumber: { type: Schema.Types.String, required: true }, | ||
idc: { type: Schema.Types.String, required: true }, | ||
indeks: { type: Schema.Types.String, required: true }, | ||
}, { | ||
collection: 'members', | ||
}); | ||
|
||
export const Member = mongoose.models.Member as MemberModel || mongoose.model<IMember, MemberModel>('Member', memberSchema); |