2020-09-05 20:35:52 +00:00
|
|
|
import {objectToCamelCase} from '@/helpers/case'
|
2021-10-06 20:25:06 +00:00
|
|
|
import {omitBy, isNil} from '@/helpers/utils'
|
2022-08-13 13:26:57 +00:00
|
|
|
import type {Right} from '@/constants/rights'
|
2019-03-02 10:25:10 +00:00
|
|
|
|
2022-07-20 22:42:36 +00:00
|
|
|
export interface IAbstract {
|
|
|
|
maxRight: Right | null
|
|
|
|
}
|
|
|
|
|
|
|
|
export default class AbstractModel implements IAbstract {
|
2019-03-02 10:25:10 +00:00
|
|
|
|
2020-08-11 18:18:59 +00:00
|
|
|
/**
|
|
|
|
* The max right the user has on this object, as returned by the x-max-right header from the api.
|
|
|
|
*/
|
2022-06-23 01:14:58 +00:00
|
|
|
maxRight: Right | null = null
|
2020-08-11 18:18:59 +00:00
|
|
|
|
2019-03-02 10:25:10 +00:00
|
|
|
/**
|
|
|
|
* The abstract constructor takes an object and merges its data with the default data of this model.
|
|
|
|
*/
|
2022-01-04 23:13:08 +00:00
|
|
|
constructor(data : Object = {}) {
|
2020-04-14 20:46:27 +00:00
|
|
|
data = objectToCamelCase(data)
|
|
|
|
|
2019-03-02 10:25:10 +00:00
|
|
|
// Put all data in our model while overriding those with a value of null or undefined with their defaults
|
2021-10-06 20:25:06 +00:00
|
|
|
Object.assign(
|
|
|
|
this,
|
|
|
|
this.defaults(),
|
|
|
|
omitBy(data, isNil),
|
|
|
|
)
|
2019-03-02 10:25:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Default attributes that define the "empty" state.
|
|
|
|
*/
|
2022-01-04 23:13:08 +00:00
|
|
|
defaults(): Object {
|
2019-03-02 10:25:10 +00:00
|
|
|
return {}
|
|
|
|
}
|
|
|
|
}
|