feat(backend): New pattern routes
This commit is contained in:
parent
248cad7720
commit
ba34d47ee0
3 changed files with 57 additions and 2 deletions
|
@ -2,6 +2,20 @@ import { PatternModel } from '../models/pattern.mjs'
|
||||||
|
|
||||||
export function PatternsController() {}
|
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
|
* Create a pattern
|
||||||
* See: https://freesewing.dev/reference/backend/api
|
* See: https://freesewing.dev/reference/backend/api
|
||||||
|
|
|
@ -13,6 +13,23 @@ export function PatternModel(tools) {
|
||||||
return this
|
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 }) {
|
PatternModel.prototype.guardedCreate = async function ({ body, user }) {
|
||||||
if (!this.rbac.user(user)) return this.setResponse(403, 'insufficientAccessLevel')
|
if (!this.rbac.user(user)) return this.setResponse(403, 'insufficientAccessLevel')
|
||||||
if (Object.keys(body).length < 2) return this.setResponse(400, 'postBodyMissing')
|
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
|
* Removes the pattern - No questions asked
|
||||||
*/
|
*/
|
||||||
PatternModel.prototype.unguardedDelete = async function () {
|
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.record = null
|
||||||
this.clear = null
|
this.clear = null
|
||||||
|
|
||||||
|
@ -256,7 +273,7 @@ PatternModel.prototype.unguardedDelete = async function () {
|
||||||
* Removes the pattern - Checks permissions
|
* Removes the pattern - Checks permissions
|
||||||
*/
|
*/
|
||||||
PatternModel.prototype.guardedDelete = async function ({ params, user }) {
|
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')
|
if (user.iss && user.status < 1) return this.setResponse(403, 'accountStatusLacking')
|
||||||
|
|
||||||
await this.read({ id: parseInt(params.id) })
|
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
|
* Helper method to set the response code, result, and body
|
||||||
*
|
*
|
||||||
|
|
|
@ -7,6 +7,14 @@ const bsc = ['basic', { session: false }]
|
||||||
export function patternsRoutes(tools) {
|
export function patternsRoutes(tools) {
|
||||||
const { app, passport } = 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
|
// Create pattern
|
||||||
app.post('/patterns/jwt', passport.authenticate(...jwt), (req, res) =>
|
app.post('/patterns/jwt', passport.authenticate(...jwt), (req, res) =>
|
||||||
Patterns.create(req, res, tools)
|
Patterns.create(req, res, tools)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue