1
0
Fork 0

feat(backend): New pattern routes

This commit is contained in:
joostdecock 2023-05-26 17:23:56 +02:00
parent 248cad7720
commit ba34d47ee0
3 changed files with 57 additions and 2 deletions

View file

@ -2,6 +2,20 @@ import { PatternModel } from '../models/pattern.mjs'
export function PatternsController() {}
/*
* Get a list of patterns
* See: https://freesewing.dev/reference/backend/api
*/
PatternsController.prototype.list = async (req, res, tools) => {
const Pattern = new PatternModel(tools)
const patterns = await Pattern.userPatterns(req.user.uid)
if (patterns) Pattern.setResponse(200, 'success', { patterns })
else Pattern.setResponse(404, 'notFound')
return Pattern.sendResponse(res)
}
/*
* Create a pattern
* See: https://freesewing.dev/reference/backend/api

View file

@ -13,6 +13,23 @@ export function PatternModel(tools) {
return this
}
/*
* Returns a list of sets for the user making the API call
*/
PatternModel.prototype.userPatterns = async function (uid) {
if (!uid) return false
let patterns
try {
patterns = await this.prisma.pattern.findMany({ where: { userId: uid } })
} catch (err) {
log.warn(`Failed to search patterns for user ${uid}: ${err}`)
}
const list = []
for (const pattern of patterns) list.push(this.revealPattern(pattern))
return list
}
PatternModel.prototype.guardedCreate = async function ({ body, user }) {
if (!this.rbac.user(user)) return this.setResponse(403, 'insufficientAccessLevel')
if (Object.keys(body).length < 2) return this.setResponse(400, 'postBodyMissing')
@ -245,7 +262,7 @@ PatternModel.prototype.guardedUpdate = async function ({ params, body, user }) {
* Removes the pattern - No questions asked
*/
PatternModel.prototype.unguardedDelete = async function () {
await this.prisma.pattern.delete({ here: { id: this.record.id } })
await this.prisma.pattern.delete({ where: { id: this.record.id } })
this.record = null
this.clear = null
@ -256,7 +273,7 @@ PatternModel.prototype.unguardedDelete = async function () {
* Removes the pattern - Checks permissions
*/
PatternModel.prototype.guardedDelete = async function ({ params, user }) {
if (this.rbac.writeSome(user)) return this.setResponse(403, 'insufficientAccessLevel')
if (!this.rbac.writeSome(user)) return this.setResponse(403, 'insufficientAccessLevel')
if (user.iss && user.status < 1) return this.setResponse(403, 'accountStatusLacking')
await this.read({ id: parseInt(params.id) })
@ -279,6 +296,22 @@ PatternModel.prototype.asPattern = function () {
}
}
/*
* Helper method to decrypt data from a non-instantiated pattern
*/
PatternModel.prototype.revealPattern = function (pattern) {
const clear = {}
for (const field of this.encryptedFields) {
try {
clear[field] = this.decrypt(pattern[field])
} catch (err) {
//console.log(err)
}
}
return { ...pattern, ...clear }
}
/*
* Helper method to set the response code, result, and body
*

View file

@ -7,6 +7,14 @@ const bsc = ['basic', { session: false }]
export function patternsRoutes(tools) {
const { app, passport } = tools
// Get patterns for the authenticate user
app.get('/patterns/jwt', passport.authenticate(...jwt), (req, res) =>
Patterns.list(req, res, tools)
)
app.get('/patterns/key', passport.authenticate(...bsc), (req, res) =>
Patterns.list(req, res, tools)
)
// Create pattern
app.post('/patterns/jwt', passport.authenticate(...jwt), (req, res) =>
Patterns.create(req, res, tools)