🔧 (validate document) add script to generate the validate function

This commit is contained in:
Julien Calixte
2020-03-01 00:19:31 +01:00
parent b06617b860
commit ef2ee8d709
4 changed files with 66 additions and 23 deletions

View File

@@ -1,21 +0,0 @@
import IAccount from '@/models/IAccount'
import ITransaction from '@/models/ITransaction'
import IExchange from '@/models/IExchange'
type Doc = IAccount | ITransaction | IExchange
interface IUserContext {
db: string
name: string | null
roles: string[]
}
export default {
validate_doc_update(newDoc: Doc, oldDoc: Doc, userCtx: IUserContext) {
if (newDoc.doctype === 'account' && oldDoc.doctype === 'account') {
if (newDoc.userIds.indexOf(userCtx.name || '') < 0) {
throw { forbidden: 'You do not have rights to update this account' }
}
}
}
}

View File

@@ -0,0 +1,31 @@
import IAccount from '@/models/IAccount'
import ITransaction from '@/models/ITransaction'
import IExchange from '@/models/IExchange'
type Doc = IAccount | ITransaction | IExchange
interface IUserContext {
db: string
name: string | null
roles: string[]
}
// tslint:disable-next-line: variable-name only-arrow-functions
const validate_doc_update = function(
newDoc: Doc,
oldDoc: Doc,
userCtx: IUserContext
) {
if (!userCtx.name) {
throw { forbidden: 'You need to be logged in to update accounts' }
}
if (newDoc.doctype === 'account' && oldDoc.doctype === 'account') {
if (true) {
throw { forbidden: 'You do not have rights to update this account' }
}
}
}
// tslint:disable-next-line:no-console
console.info(validate_doc_update.toString().replace(/\n/g, ''))