diff --git a/src/couch-design/validate-doc-update.ts b/src/couch-design/validate-doc-update.ts new file mode 100644 index 0000000..5af7ffa --- /dev/null +++ b/src/couch-design/validate-doc-update.ts @@ -0,0 +1,21 @@ +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' } + } + } + } +} diff --git a/src/models/IAccount.ts b/src/models/IAccount.ts index c25d8f6..4d35e82 100644 --- a/src/models/IAccount.ts +++ b/src/models/IAccount.ts @@ -3,6 +3,7 @@ import IUser from '@/models/IUser' import ICurrency from '@/models/ICurrency' export default interface IAccount extends IModel { + doctype: 'account' name: string color: string | null slug?: string diff --git a/src/models/IExchange.ts b/src/models/IExchange.ts index c9ea731..a428288 100644 --- a/src/models/IExchange.ts +++ b/src/models/IExchange.ts @@ -1,6 +1,7 @@ import IModel from '@/models/IModel' export default interface IExchange extends IModel { + doctype: 'exchange' base: string date: string timestamp?: string diff --git a/src/models/IModel.ts b/src/models/IModel.ts index 6819f9a..8ccc0a9 100644 --- a/src/models/IModel.ts +++ b/src/models/IModel.ts @@ -3,5 +3,4 @@ export default interface IModel { _rev?: string _deleted?: boolean _conflicts?: string[] - doctype?: string } diff --git a/src/models/ITransaction.ts b/src/models/ITransaction.ts index 629624f..c2311de 100644 --- a/src/models/ITransaction.ts +++ b/src/models/ITransaction.ts @@ -7,6 +7,7 @@ import ISplit from '@/models/ISplit' import ILocation from './ILocation' export default interface ITransaction extends IModel { + doctype: 'transaction' name: string accountId: string date: Date diff --git a/src/services/ExchangeService.ts b/src/services/ExchangeService.ts index 9ad942c..40b294e 100644 --- a/src/services/ExchangeService.ts +++ b/src/services/ExchangeService.ts @@ -35,6 +35,7 @@ class ExchangeService { if (!needFetch) { exchange = { base, + doctype: 'exchange', date: formattedDate, rates: { [base]: 1