forked from b00tc4mp/isdi-parttime-202309
-
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.
add redeemPoints logic b00tc4mp#431; improve delayTask logic b00tc4mp…
- Loading branch information
Showing
15 changed files
with
194 additions
and
15 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
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
38 changes: 38 additions & 0 deletions
38
staff/miguel-arias/project/api/handlers/redeemPointsHandler.js
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,38 @@ | ||
import jwt from 'jsonwebtoken' | ||
const {JsonWebTokenError} = jwt | ||
|
||
import { errors } from 'com' | ||
const { NotFoundError, ContentError, TokenError } = errors | ||
|
||
import logic from '../logic/index.js' | ||
|
||
export default async (req, res) => { | ||
const token = req.headers.authorization.substring(7) | ||
const payload = jwt.verify(token, process.env.JWT_SECRET) | ||
const sessionProfileId = payload.sub | ||
|
||
const { points } = req.body | ||
|
||
const { profileId } = req.params | ||
|
||
try { | ||
await logic.redeemPoints(sessionProfileId, profileId, points) | ||
} catch (error) { | ||
let status = 500 | ||
|
||
if (error instanceof JsonWebTokenError) { | ||
status = 401 | ||
error = new TokenError(error.message) | ||
} | ||
|
||
if (error instanceof NotFoundError) | ||
status = 404 | ||
|
||
if (error instanceof ContentError || error instanceof TypeError) | ||
status = 406 | ||
|
||
res.status(status).json({ error: error.constructor.name, message: error.message }) | ||
} | ||
|
||
res.status(204).send() | ||
} |
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,45 @@ | ||
import { validate, errors } from 'com' | ||
const { SystemError, NotFoundError, PermissionError } = errors | ||
|
||
import { Task, Profile } from '../data/models.js' | ||
|
||
function delayTask(sessionProfileId, profileId, points) { | ||
validate.id(sessionProfileId, 'session profile id') | ||
validate.id(profileId, 'profile id') | ||
validate.number(points, 'points') | ||
|
||
return (async () => { | ||
let sessionProfile | ||
try { | ||
sessionProfile = await Profile.findById(sessionProfileId).lean() | ||
} catch (error) { | ||
throw new SystemError(error.message) | ||
} | ||
|
||
if (!sessionProfile) | ||
throw new NotFoundError('session profile not found') | ||
|
||
if (sessionProfile.role !== 'admin') | ||
throw new PermissionError('session profile is not admin') | ||
|
||
let profile | ||
try { | ||
profile = await Profile.findById(profileId) | ||
} catch (error) { | ||
throw new SystemError(error.message) | ||
} | ||
|
||
if (!profile) | ||
throw new NotFoundError('profile not found') | ||
|
||
profile.points -= points | ||
|
||
try { | ||
await profile.save() | ||
} catch (error) { | ||
throw new SystemError(error.message) | ||
} | ||
})() | ||
} | ||
|
||
export default delayTask |
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 mongoose from 'mongoose' | ||
import redeemPoints from './redeemPoints.js' | ||
|
||
(async () => { | ||
try { | ||
await mongoose.connect('mongodb://127.0.0.1:27017/test') | ||
|
||
await redeemPoints('65d79ed43377222a97582a1a', '65d79ed33377222a97582a18', 15) | ||
|
||
console.log('points redeemed') | ||
|
||
await mongoose.disconnect() | ||
} catch (error) { | ||
console.log(error) | ||
} | ||
})() |
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
18 changes: 11 additions & 7 deletions
18
staff/miguel-arias/project/app/src/components/Template.jsx
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,41 @@ | ||
import { validate, errors } from 'com' | ||
const { SystemError } = errors | ||
|
||
import session from './session' | ||
|
||
const redeemPoints = (profileId, points) => { | ||
validate.id(profileId, 'profile id') | ||
validate.number(points, 'points') | ||
|
||
const req = { | ||
method: 'PATCH', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
Authorization: `Bearer ${session.profileToken}` | ||
}, | ||
body: JSON.stringify({ points }) | ||
} | ||
|
||
return (async () => { | ||
let res | ||
try { | ||
res = await fetch(`${import.meta.env.VITE_API_URL}/stats/${profileId}/redeem`, req) | ||
} catch (error) { | ||
throw new SystemError(error.message) | ||
} | ||
|
||
if (!res.ok) { | ||
let body | ||
|
||
try { | ||
body = await res.json() | ||
} catch (error) { | ||
throw new SystemError(error.message) | ||
} | ||
|
||
throw new errors[body.error](body.message) | ||
} | ||
})() | ||
} | ||
|
||
export default redeemPoints |
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