From 8416b1f44811ff477d81db20370ff68e899c7252 Mon Sep 17 00:00:00 2001 From: Dominik Pschenitschni Date: Sun, 14 Aug 2022 12:15:45 +0200 Subject: [PATCH] fix: use new assignData method for default data --- src/models/abstractModel.ts | 21 ++---- src/models/attachment.ts | 26 +++----- src/models/avatar.ts | 9 ++- src/models/backgroundImage.ts | 23 +++---- src/models/bucket.ts | 41 ++++-------- src/models/caldavToken.ts | 11 ++-- src/models/emailUpdate.ts | 12 ++-- src/models/file.ts | 26 +++----- src/models/label.ts | 47 +++++--------- src/models/labelTask.ts | 15 ++--- src/models/linkShare.ts | 43 ++++--------- src/models/list.ts | 62 ++++++------------ src/models/listDuplicateModel.ts | 20 ++---- src/models/namespace.ts | 42 ++++-------- src/models/notification.ts | 28 +++----- src/models/passwordReset.ts | 19 ++---- src/models/passwordUpdate.ts | 12 ++-- src/models/savedFilter.ts | 50 ++++++--------- src/models/subscription.ts | 25 +++----- src/models/task.ts | 107 ++++++++++--------------------- src/models/taskAssignee.ts | 19 ++---- src/models/taskComment.ts | 29 +++------ src/models/taskRelation.ts | 30 +++------ src/models/team.ts | 35 ++++------ src/models/teamList.ts | 10 ++- src/models/teamMember.ts | 13 ++-- src/models/teamNamespace.ts | 10 ++- src/models/teamShareBase.ts | 24 +++---- src/models/totp.ts | 17 ++--- src/models/user.ts | 32 +++------ src/models/userList.ts | 10 ++- src/models/userNamespace.ts | 10 ++- src/models/userSettings.ts | 30 ++++----- src/models/userShareBase.ts | 24 +++---- 34 files changed, 317 insertions(+), 615 deletions(-) diff --git a/src/models/abstractModel.ts b/src/models/abstractModel.ts index 9592ec55..f859c361 100644 --- a/src/models/abstractModel.ts +++ b/src/models/abstractModel.ts @@ -12,25 +12,14 @@ export default abstract class AbstractModel * The max right the user has on this object, as returned by the x-max-right header from the api. */ maxRight: Right | null = null - + /** - * The abstract constructor takes an object and merges its data with the default data of this model. - */ - constructor(data : Object = {}) { + * Takes an object and merges its data with the default data of this model. + */ + assignData(data: Partial) { data = objectToCamelCase(data) // Put all data in our model while overriding those with a value of null or undefined with their defaults - Object.assign( - this, - this.defaults(), - omitBy(data, isNil), - ) - } - - /** - * Default attributes that define the "empty" state. - */ - defaults(): Object { - return {} + Object.assign(this, omitBy(data, isNil)) } } \ No newline at end of file diff --git a/src/models/attachment.ts b/src/models/attachment.ts index 8c48a17d..074d61b6 100644 --- a/src/models/attachment.ts +++ b/src/models/attachment.ts @@ -11,26 +11,18 @@ export interface IAttachment extends IAbstract { } export default class AttachmentModel extends AbstractModel implements IAttachment { - id!: number - taskId!: number - createdBy: IUser - file: IFile - created: Date + id = 0 + taskId = 0 + createdBy: IUser = UserModel + file: IFile = FileModel + created: Date = null + + constructor(data: Partial) { + super() + this.assignData(data) - constructor(data) { - super(data) this.createdBy = new UserModel(this.createdBy) this.file = new FileModel(this.file) this.created = new Date(this.created) } - - defaults() { - return { - id: 0, - taskId: 0, - createdBy: UserModel, - file: FileModel, - created: null, - } - } } diff --git a/src/models/avatar.ts b/src/models/avatar.ts index 43d49151..daf221c0 100644 --- a/src/models/avatar.ts +++ b/src/models/avatar.ts @@ -7,11 +7,10 @@ export interface IAvatar extends IAbstract { } export default class AvatarModel extends AbstractModel implements IAvatar { - avatarProvider!: AvatarProvider + avatarProvider: AvatarProvider = 'default' - defaults() { - return { - avatarProvider: '', - } + constructor(data: Partial) { + super() + this.assignData(data) } } \ No newline at end of file diff --git a/src/models/backgroundImage.ts b/src/models/backgroundImage.ts index f56e750c..137c8383 100644 --- a/src/models/backgroundImage.ts +++ b/src/models/backgroundImage.ts @@ -12,22 +12,17 @@ export interface IBackgroundImage extends IAbstract { } export default class BackgroundImageModel extends AbstractModel implements IBackgroundImage { - id!: number - url!: string - thumb!: string - info!: { + id = 0 + url = '' + thumb = '' + info: { author: string authorName: string - } - blurHash!: string + } = {} + blurHash = '' - defaults() { - return { - id: 0, - url: '', - thumb: '', - info: {}, - blurHash: '', - } + constructor(data: Partial) { + super() + this.assignData(data) } } \ No newline at end of file diff --git a/src/models/bucket.ts b/src/models/bucket.ts index 1d89c86f..94f6c2d0 100644 --- a/src/models/bucket.ts +++ b/src/models/bucket.ts @@ -17,20 +17,21 @@ export interface IBucket extends IAbstract { } export default class BucketModel extends AbstractModel implements IBucket { - id!: number - title!: string - listId!: number - limit!: number - tasks!: ITask[] - isDoneBucket!: boolean - position!: number + id = 0 + title = '' + listId = '' + limit = 0 + tasks: ITask[] = [] + isDoneBucket: false + position: 0 - createdBy: IUser - created: Date - updated: Date + createdBy: IUser = null + created: Date = null + updated: Date = null - constructor(bucket) { - super(bucket) + constructor(data: Partial) { + super() + this.assignData(data) this.tasks = this.tasks.map(t => new TaskModel(t)) @@ -38,20 +39,4 @@ export default class BucketModel extends AbstractModel implements IBucket { this.created = new Date(this.created) this.updated = new Date(this.updated) } - - defaults() { - return { - id: 0, - title: '', - listId: 0, - limit: 0, - tasks: [], - isDoneBucket: false, - position: 0, - - createdBy: null, - created: null, - updated: null, - } - } } \ No newline at end of file diff --git a/src/models/caldavToken.ts b/src/models/caldavToken.ts index a2e53c98..4a4d4c4d 100644 --- a/src/models/caldavToken.ts +++ b/src/models/caldavToken.ts @@ -6,14 +6,13 @@ export interface ICaldavToken extends IAbstract { } export default class CaldavTokenModel extends AbstractModel implements ICaldavToken { - id!: number - created!: Date + id: number + created: Date - constructor(data? : Object) { - super(data) + constructor(data? : Partial) { + super() + this.assignData(data) - this.id - if (this.created) { this.created = new Date(this.created) } diff --git a/src/models/emailUpdate.ts b/src/models/emailUpdate.ts index aef40a12..2f8c9a85 100644 --- a/src/models/emailUpdate.ts +++ b/src/models/emailUpdate.ts @@ -6,13 +6,11 @@ interface IEmailUpdate extends IAbstract { } export default class EmailUpdateModel extends AbstractModel implements IEmailUpdate { - newEmail!: string - password!: string + newEmail = '' + password = '' - defaults() { - return { - newEmail: '', - password: '', - } + constructor(data : Partial) { + super() + this.assignData(data) } } \ No newline at end of file diff --git a/src/models/file.ts b/src/models/file.ts index 0a768743..ba5950c7 100644 --- a/src/models/file.ts +++ b/src/models/file.ts @@ -9,27 +9,19 @@ export interface IFile extends IAbstract { } export default class FileModel extends AbstractModel implements IFile { - id!: number - mime!: string - name!: string - size!: number - created: Date + id = 0 + mime = '' + name = '' + size = 0 + created: Date = null + + constructor(data: Partial) { + super() + this.assignData(data) - constructor(data) { - super(data) this.created = new Date(this.created) } - defaults() { - return { - id: 0, - mime: '', - name: '', - size: 0, - created: null, - } - } - getHumanSize() { const sizes = { 0: 'B', diff --git a/src/models/label.ts b/src/models/label.ts index 48642e39..5b3659b7 100644 --- a/src/models/label.ts +++ b/src/models/label.ts @@ -18,25 +18,23 @@ export interface ILabel extends IAbstract { } export default class LabelModel extends AbstractModel implements ILabel { - id!: number - title!: string - hexColor!: string - description!: string - createdBy!: IUser - listId!: number - textColor!: string + id = 0 + title = '' + // FIXME: this should be empty and be definied in the client. + // that way it get's never send to the server db and is easier to change in future versions. + hexColor = DEFAULT_LABEL_BACKGROUND_COLOR + description = '' + createdBy: IUser + listId = 0 + textColor = '' - created: Date - updated: Date + created: Date = null + updated: Date = null + + constructor(data: Partial) { + super() + this.assignData(data) - constructor(data) { - super(data) - // FIXME: this should be empty and be definied in the client. - // that way it get's never send to the server db and is easier to change in future versions. - // Set the default color - if (this.hexColor === '') { - this.hexColor = DEFAULT_LABEL_BACKGROUND_COLOR - } if (this.hexColor.substring(0, 1) !== '#') { this.hexColor = '#' + this.hexColor } @@ -46,19 +44,4 @@ export default class LabelModel extends AbstractModel implements ILabel { this.created = new Date(this.created) this.updated = new Date(this.updated) } - - defaults() { - return { - id: 0, - title: '', - hexColor: '', - description: '', - createdBy: UserModel, - listId: 0, - textColor: '', - - created: null, - updated: null, - } - } } \ No newline at end of file diff --git a/src/models/labelTask.ts b/src/models/labelTask.ts index 46a5b137..8223edb4 100644 --- a/src/models/labelTask.ts +++ b/src/models/labelTask.ts @@ -7,15 +7,12 @@ export interface ILabelTask extends IAbstract { } export default class LabelTask extends AbstractModel implements ILabelTask { - id!: number - taskId!: number - labelId!: number + id = 0 + taskId = 0 + labelId = 0 - defaults() { - return { - id: 0, - taskId: 0, - labelId: 0, - } + constructor(data: Partial) { + super() + this.assignData(data) } } \ No newline at end of file diff --git a/src/models/linkShare.ts b/src/models/linkShare.ts index 08f67a3e..c7df693b 100644 --- a/src/models/linkShare.ts +++ b/src/models/linkShare.ts @@ -16,41 +16,24 @@ export interface ILinkShare extends IAbstract { } export default class LinkShareModel extends AbstractModel implements ILinkShare { - id!: number - hash!: string - right!: Right - sharedBy: IUser - sharingType!: number // FIXME: use correct numbers - listId!: number - name!: string - password!: string - created: Date - updated: Date + id = 0 + hash = '' + right: Right = RIGHTS.READ + sharedBy: IUser = UserModel + sharingType = 0 // FIXME: use correct numbers + listId = 0 + name: '' + password: '' + created: Date = null + updated: Date = null - constructor(data) { - // The constructor of AbstractModel handles all the default parsing. - super(data) + constructor(data: Partial) { + super() + this.assignData(data) this.sharedBy = new UserModel(this.sharedBy) this.created = new Date(this.created) this.updated = new Date(this.updated) } - - // Default attributes that define the "empty" state. - defaults() { - return { - id: 0, - hash: '', - right: RIGHTS.READ, - sharedBy: UserModel, - sharingType: 0, - listId: 0, - name: '', - password: '', - - created: null, - updated: null, - } - } } \ No newline at end of file diff --git a/src/models/list.ts b/src/models/list.ts index 496737a1..7ef058c4 100644 --- a/src/models/list.ts +++ b/src/models/list.ts @@ -16,7 +16,7 @@ export interface IList extends IAbstract { isArchived: boolean hexColor: string identifier: string - backgroundInformation: any + backgroundInformation: any // FIXME: improve type isFavorite: boolean subscription: ISubscription position: number @@ -27,26 +27,27 @@ export interface IList extends IAbstract { } export default class ListModel extends AbstractModel implements IList { - id!: number - title!: string - description!: string - owner: IUser - tasks: ITask[] - namespaceId!: INamespace['id'] - isArchived!: boolean - hexColor!: string - identifier!: string - backgroundInformation!: any - isFavorite!: boolean - subscription!: ISubscription - position!: number - backgroundBlurHash!: string + id = 0 + title = '' + description = '' + owner: IUser = UserModel + tasks: ITask[] = [] + namespaceId: INamespace['id'] = 0 + isArchived = false + hexColor = '' + identifier = '' + backgroundInformation: any = null + isFavorite = false + subscription: ISubscription = null + position = 0 + backgroundBlurHash = '' - created: Date - updated: Date + created: Date = null + updated: Date = null - constructor(data) { - super(data) + constructor(data: Partial) { + super() + this.assignData(data) this.owner = new UserModel(this.owner) @@ -67,29 +68,6 @@ export default class ListModel extends AbstractModel implements IList { this.updated = new Date(this.updated) } - // Default attributes that define the "empty" state. - defaults() { - return { - id: 0, - title: '', - description: '', - owner: UserModel, - tasks: [], - namespaceId: 0, - isArchived: false, - hexColor: '', - identifier: '', - backgroundInformation: null, - isFavorite: false, - subscription: null, - position: 0, - backgroundBlurHash: '', - - created: null, - updated: null, - } - } - isSavedFilter() { return this.getSavedFilterId() > 0 } diff --git a/src/models/listDuplicateModel.ts b/src/models/listDuplicateModel.ts index d3b199d4..4d8faf1f 100644 --- a/src/models/listDuplicateModel.ts +++ b/src/models/listDuplicateModel.ts @@ -9,20 +9,14 @@ export interface IListDuplicate extends IAbstract { } export default class ListDuplicateModel extends AbstractModel implements IListDuplicate { - listId!: number - namespaceId!: INamespace['id'] - list: IList + listId = 0 + namespaceId: INamespace['id'] = 0 + list: IList = ListModel + + constructor(data : Partial) { + super() + this.assignData(data) - constructor(data) { - super(data) this.list = new ListModel(this.list) } - - defaults() { - return { - listId: 0, - namespaceId: 0, - list: ListModel, - } - } } \ No newline at end of file diff --git a/src/models/namespace.ts b/src/models/namespace.ts index 802e8517..f1962f1a 100644 --- a/src/models/namespace.ts +++ b/src/models/namespace.ts @@ -18,20 +18,21 @@ export interface INamespace extends IAbstract { } export default class NamespaceModel extends AbstractModel implements INamespace { - id!: number - title!: string - description!: string - owner: IUser - lists: IList[] - isArchived!: boolean - hexColor!: string - subscription!: ISubscription + id = 0 + title = '' + description = '' + owner: IUser = UserModel + lists: IList[] = [] + isArchived = false + hexColor = '' + subscription: ISubscription = null - created: Date - updated: Date + created: Date = null + updated: Date = null - constructor(data) { - super(data) + constructor(data: Partial) { + super() + this.assignData(data) if (this.hexColor !== '' && this.hexColor.substring(0, 1) !== '#') { this.hexColor = '#' + this.hexColor @@ -50,21 +51,4 @@ export default class NamespaceModel extends AbstractModel implements INamespace this.created = new Date(this.created) this.updated = new Date(this.updated) } - - // Default attributes that define the 'empty' state. - defaults() { - return { - id: 0, - title: '', - description: '', - owner: UserModel, - lists: [], - isArchived: false, - hexColor: '', - subscription: null, - - created: null, - updated: null, - } - } } diff --git a/src/models/notification.ts b/src/models/notification.ts index 94b65bc9..bd9c8727 100644 --- a/src/models/notification.ts +++ b/src/models/notification.ts @@ -3,7 +3,7 @@ import {parseDateOrNull} from '@/helpers/parseDateOrNull' import UserModel, { type IUser } from '@/models/user' import TaskModel, { type ITask } from '@/models/task' import TaskCommentModel, { type ITaskComment } from '@/models/taskComment' -import ListModel from '@/models/list' +import ListModel, { type IList } from '@/models/list' import TeamModel, { type ITeam } from '@/models/team' export const NOTIFICATION_NAMES = { @@ -33,6 +33,7 @@ interface NotificationDeleted extends Notification { interface NotificationCreated extends Notification { task: ITask + list: IList } interface NotificationMemberAdded extends Notification { @@ -51,16 +52,17 @@ export interface INotification extends IAbstract { } export default class NotificationModel extends AbstractModel implements INotification { - id!: number - name!: string - notification!: NotificationTask | NotificationAssigned | NotificationDeleted | NotificationCreated | NotificationMemberAdded - read!: boolean - readAt: Date | null + id = 0 + name = '' + notification: NotificationTask | NotificationAssigned | NotificationDeleted | NotificationCreated | NotificationMemberAdded = null + read = false + readAt: Date | null = null created: Date - constructor(data) { - super(data) + constructor(data: Partial) { + super() + this.assignData(data) switch (this.name) { case NOTIFICATION_NAMES.TASK_COMMENT: @@ -102,16 +104,6 @@ export default class NotificationModel extends AbstractModel implements INotific this.readAt = parseDateOrNull(this.readAt) } - defaults() { - return { - id: 0, - name: '', - notification: null, - read: false, - readAt: null, - } - } - toText(user = null) { let who = '' diff --git a/src/models/passwordReset.ts b/src/models/passwordReset.ts index 696fae26..e982f7cd 100644 --- a/src/models/passwordReset.ts +++ b/src/models/passwordReset.ts @@ -7,21 +7,14 @@ export interface IPasswordReset extends IAbstract { } export default class PasswordResetModel extends AbstractModel implements IPasswordReset { - token: string - newPassword!: string - email!: string + token = '' + newPassword = '' + email = '' - constructor(data) { - super(data) + constructor(data: Partial) { + super() + this.assignData(data) this.token = localStorage.getItem('passwordResetToken') } - - defaults() { - return { - token: '', - newPassword: '', - email: '', - } - } } \ No newline at end of file diff --git a/src/models/passwordUpdate.ts b/src/models/passwordUpdate.ts index f4f04553..1548ccb9 100644 --- a/src/models/passwordUpdate.ts +++ b/src/models/passwordUpdate.ts @@ -6,13 +6,11 @@ export interface IPasswordUpdate extends IAbstract { } export default class PasswordUpdateModel extends AbstractModel implements IPasswordUpdate { - newPassword!: string - oldPassword!: string + newPassword = '' + oldPassword = '' - defaults() { - return { - newPassword: '', - oldPassword: '', - } + constructor(data: Partial) { + super() + this.assignData(data) } } \ No newline at end of file diff --git a/src/models/savedFilter.ts b/src/models/savedFilter.ts index 7c6ac36b..8749d035 100644 --- a/src/models/savedFilter.ts +++ b/src/models/savedFilter.ts @@ -2,7 +2,7 @@ import AbstractModel, { type IAbstract } from '@/models/abstractModel' import UserModel, { type IUser } from '@/models/user' export interface ISavedFilter extends IAbstract { - id: 0 + id: number title: string description: string filters: { @@ -21,10 +21,10 @@ export interface ISavedFilter extends IAbstract { } export default class SavedFilterModel extends AbstractModel implements ISavedFilter { - id!: 0 - title!: string - description!: string - filters!: { + id = 0 + title = '' + description = '' + filters: { sortBy: ('done' | 'id')[] orderBy: ('asc' | 'desc')[] filterBy: 'done'[] @@ -32,14 +32,23 @@ export default class SavedFilterModel extends AbstractModel implements ISavedFil filterComparator: 'equals'[] filterConcat: 'and' filterIncludeNulls: boolean + } = { + sortBy: ['done', 'id'], + orderBy: ['asc', 'desc'], + filterBy: ['done'], + filterValue: ['false'], + filterComparator: ['equals'], + filterConcat: 'and', + filterIncludeNulls: true, } - owner: IUser - created: Date - updated: Date + owner: IUser = {} + created: Date = null + updated: Date = null - constructor(data) { - super(data) + constructor(data: Partial) { + super() + this.assignData(data) this.owner = new UserModel(this.owner) @@ -47,27 +56,6 @@ export default class SavedFilterModel extends AbstractModel implements ISavedFil this.updated = new Date(this.updated) } - defaults() { - return { - id: 0, - title: '', - description: '', - filters: { - sortBy: ['done', 'id'], - orderBy: ['asc', 'desc'], - filterBy: ['done'], - filterValue: ['false'], - filterComparator: ['equals'], - filterConcat: 'and', - filterIncludeNulls: true, - }, - - owner: {}, - created: null, - updated: null, - } - } - /** * Calculates the corresponding list id to this saved filter. * This function matches the one in the api. diff --git a/src/models/subscription.ts b/src/models/subscription.ts index 459c78d3..3ae79dd2 100644 --- a/src/models/subscription.ts +++ b/src/models/subscription.ts @@ -11,27 +11,18 @@ export interface ISubscription extends IAbstract { } export default class SubscriptionModel extends AbstractModel implements ISubscription { - id!: number - entity!: string // FIXME: correct type? - entityId!: number // FIXME: correct type? - user: IUser + id = 0 + entity = '' + entityId = 0 + user: IUser = {} - created: Date + created: Date = null - constructor(data) { - super(data) + constructor(data : Partial) { + super() + this.assignData(data) this.created = new Date(this.created) this.user = new UserModel(this.user) } - - defaults() { - return { - id: 0, - entity: '', - entityId: 0, - created: null, - user: {}, - } - } } diff --git a/src/models/task.ts b/src/models/task.ts index 8d450cc4..e3c03b13 100644 --- a/src/models/task.ts +++ b/src/models/task.ts @@ -1,4 +1,4 @@ -import type { Priority } from '@/constants/priorities' +import { PRIORITIES, type Priority } from '@/constants/priorities' import AbstractModel, { type IAbstract } from '@/models/abstractModel' import UserModel, { type IUser } from '@/models/user' @@ -61,44 +61,45 @@ export interface ITask extends IAbstract { } export default class TaskModel extends AbstractModel implements ITask { - id: number - title: string - description!: string - done!: boolean - doneAt: Date | null - priority!: Priority - labels: ILabel[] - assignees: IUser[] + id = 0 + title = '' + description = '' + done = false + doneAt: Date | null = null + priority: Priority = PRIORITIES.UNSET + labels: ILabel[] = [] + assignees: IUser[] = [] - dueDate: Date | null - startDate: Date | null - endDate: Date | null - repeatAfter!: number | IRepeats - repeatFromCurrentDate!: boolean - repeatMode!: TaskRepeatMode - reminderDates: Date[] - parentTaskId!: ITask['id'] - hexColor!: string - percentDone!: number - relatedTasks!: { [relationKind: string]: ITask } // FIXME: use relationKinds - attachments: IAttachment[] - identifier!: string - index!: number - isFavorite!: boolean - subscription!: ISubscription + dueDate: Date | null = 0 + startDate: Date | null = 0 + endDate: Date | null = 0 + repeatAfter: number | IRepeats = 0 + repeatFromCurrentDate = false + repeatMode: TaskRepeatMode = TASK_REPEAT_MODES.REPEAT_MODE_DEFAULT + reminderDates: Date[] = [] + parentTaskId: ITask['id'] = 0 + hexColor = '' + percentDone = 0 + relatedTasks: { [relationKind: string]: ITask } = {} + attachments: IAttachment[] = [] + identifier = '' + index = 0 + isFavorite = false + subscription: ISubscription = null - position!: number - kanbanPosition!: number + position = 0 + kanbanPosition = 0 - createdBy: IUser - created: Date - updated: Date + createdBy: IUser = UserModel + created: Date = null + updated: Date = null - listId: IList['id'] // Meta, only used when creating a new task - bucketId!: IBucket['id'] + listId: IList['id'] = 0 + bucketId: IBucket['id'] = 0 constructor(data: Partial) { - super(data) + super() + this.assignData(data) this.id = Number(this.id) this.title = this.title?.trim() @@ -158,46 +159,6 @@ export default class TaskModel extends AbstractModel implements ITask { this.listId = Number(this.listId) } - defaults() { - return { - id: 0, - title: '', - description: '', - done: false, - doneAt: null, - priority: 0, - labels: [], - assignees: [], - - dueDate: 0, - startDate: 0, - endDate: 0, - repeatAfter: 0, - repeatFromCurrentDate: false, - repeatMode: TASK_REPEAT_MODES.REPEAT_MODE_DEFAULT, - reminderDates: [], - parentTaskId: 0, - hexColor: '', - percentDone: 0, - relatedTasks: {}, - attachments: [], - identifier: '', - index: 0, - isFavorite: false, - subscription: null, - - position: 0, - kanbanPosition: 0, - - createdBy: UserModel, - created: null, - updated: null, - - listId: 0, // Meta, only used when creating a new task - bucketId: 0, - } - } - getTextIdentifier() { if (this.identifier === '') { return `#${this.index}` diff --git a/src/models/taskAssignee.ts b/src/models/taskAssignee.ts index 93b9434f..29a8db27 100644 --- a/src/models/taskAssignee.ts +++ b/src/models/taskAssignee.ts @@ -9,20 +9,13 @@ export interface ITaskAssignee extends IAbstract { } export default class TaskAssigneeModel extends AbstractModel implements ITaskAssignee { - created: Date - userId!: IUser['id'] - taskId!: ITask['id'] + created: Date = null + userId: IUser['id'] = 0 + taskId: ITask['id'] = 0 - constructor(data) { - super(data) + constructor(data: Partial) { + super() + this.assignData(data) this.created = new Date(this.created) } - - defaults() { - return { - created: null, - userId: 0, - taskId: 0, - } - } } diff --git a/src/models/taskComment.ts b/src/models/taskComment.ts index d774d7a0..bb5ceb10 100644 --- a/src/models/taskComment.ts +++ b/src/models/taskComment.ts @@ -13,29 +13,20 @@ export interface ITaskComment extends IAbstract { } export default class TaskCommentModel extends AbstractModel implements ITaskComment { - id!: number - taskId!: ITask['id'] - comment!: string - author: IUser + id = 0 + taskId: ITask['id'] = 0 + comment = '' + author: IUser = UserModel - created: Date - updated: Date + created: Date = null + updated: Date = null + + constructor(data: Partial) { + super() + this.assignData(data) - constructor(data) { - super(data) this.author = new UserModel(this.author) this.created = new Date(this.created) this.updated = new Date(this.updated) } - - defaults() { - return { - id: 0, - taskId: 0, - comment: '', - author: UserModel, - created: null, - updated: null, - } - } } diff --git a/src/models/taskRelation.ts b/src/models/taskRelation.ts index 931c5f15..db375fab 100644 --- a/src/models/taskRelation.ts +++ b/src/models/taskRelation.ts @@ -30,29 +30,19 @@ export interface ITaskRelation extends IAbstract { } export default class TaskRelationModel extends AbstractModel implements ITaskRelation { - id!: number - otherTaskId!: ITask['id'] - taskId!: ITask['id'] - relationKind!: RelationKind + id = 0 + otherTaskId: ITask['id'] = 0 + taskId: ITask['id'] = 0 + relationKind: RelationKind = '' - createdBy: IUser - created: Date + createdBy: IUser = UserModel + created: Date = null + + constructor(data: Partial) { + super() + this.assignData(data) - constructor(data) { - super(data) this.createdBy = new UserModel(this.createdBy) this.created = new Date(this.created) } - - defaults() { - return { - id: 0, - otherTaskId: 0, - taskId: 0, - relationKind: '', - - createdBy: UserModel, - created: null, - } - } } \ No newline at end of file diff --git a/src/models/team.ts b/src/models/team.ts index c24c1352..56e13dee 100644 --- a/src/models/team.ts +++ b/src/models/team.ts @@ -16,18 +16,19 @@ export interface ITeam extends IAbstract { } export default class TeamModel extends AbstractModel implements ITeam { - id!: number - name!: string - description!: string - members: ITeamMember[] - right!: Right + id = 0 + name = '' + description = '' + members: ITeamMember[] = [] + right: Right = RIGHTS.READ - createdBy: IUser - created: Date - updated: Date + createdBy: IUser = {} // FIXME: seems wrong + created: Date = null + updated: Date = null - constructor(data) { - super(data) + constructor(data: Partial) { + super() + this.assignData(data) // Make the members to usermodels this.members = this.members.map(m => { @@ -38,18 +39,4 @@ export default class TeamModel extends AbstractModel implements ITeam { this.created = new Date(this.created) this.updated = new Date(this.updated) } - - defaults() { - return { - id: 0, - name: '', - description: '', - members: [], - right: RIGHTS.READ, - - createdBy: {}, - created: null, - updated: null, - } - } } \ No newline at end of file diff --git a/src/models/teamList.ts b/src/models/teamList.ts index 472cd479..bb55b1cc 100644 --- a/src/models/teamList.ts +++ b/src/models/teamList.ts @@ -6,12 +6,10 @@ export interface ITeamList extends TeamShareBaseModel { } export default class TeamListModel extends TeamShareBaseModel implements ITeamList { - listId!: IList['id'] + listId: IList['id'] = 0 - defaults() { - return { - ...super.defaults(), - listId: 0, - } + constructor(data: Partial) { + super(data) + this.assignData(data) } } \ No newline at end of file diff --git a/src/models/teamMember.ts b/src/models/teamMember.ts index 597599a7..6caf4932 100644 --- a/src/models/teamMember.ts +++ b/src/models/teamMember.ts @@ -7,14 +7,11 @@ export interface ITeamMember extends UserModel { } export default class TeamMemberModel extends UserModel implements ITeamMember { - admin!: boolean - teamId!: IList['id'] + admin = false + teamId: IList['id'] = 0 - defaults() { - return { - ...super.defaults(), - admin: false, - teamId: 0, - } + constructor(data: Partial) { + super(data) + this.assignData(data) } } \ No newline at end of file diff --git a/src/models/teamNamespace.ts b/src/models/teamNamespace.ts index 31a84510..104d7a77 100644 --- a/src/models/teamNamespace.ts +++ b/src/models/teamNamespace.ts @@ -6,12 +6,10 @@ export interface ITeamNamespace extends TeamShareBaseModel { } export default class TeamNamespaceModel extends TeamShareBaseModel implements ITeamNamespace { - namespaceId!: INamespace['id'] + namespaceId: INamespace['id'] = 0 - defaults() { - return { - ...super.defaults(), - namespaceId: 0, - } + constructor(data: Partial) { + super(data) + this.assignData(data) } } \ No newline at end of file diff --git a/src/models/teamShareBase.ts b/src/models/teamShareBase.ts index 7ce56364..02495d1c 100644 --- a/src/models/teamShareBase.ts +++ b/src/models/teamShareBase.ts @@ -15,25 +15,17 @@ export interface ITeamShareBase extends IAbstract { * It is extended in a way so it can be used for namespaces as well for lists. */ export default class TeamShareBaseModel extends AbstractModel implements ITeamShareBase { - teamId!: ITeam['id'] - right!: Right + teamId: ITeam['id'] = 0 + right: Right = RIGHTS.READ - created: Date - updated: Date + created: Date = null + updated: Date = null + + constructor(data: Partial) { + super() + this.assignData(data) - constructor(data) { - super(data) this.created = new Date(this.created) this.updated = new Date(this.updated) } - - defaults() { - return { - teamId: 0, - right: RIGHTS.READ, - - created: null, - updated: null, - } - } } \ No newline at end of file diff --git a/src/models/totp.ts b/src/models/totp.ts index 7d33ebd3..42242cf0 100644 --- a/src/models/totp.ts +++ b/src/models/totp.ts @@ -6,16 +6,13 @@ export interface ITotp extends IAbstract { url: string } -export default class TotpModel extends AbstractModel implements ITotp{ - secret!: string - enabled!: boolean - url!: string +export default class TotpModel extends AbstractModel implements ITotp { + secret = '' + enabled = false + url = '' - defaults() { - return { - secret: '', - enabled: false, - url: '', - } + constructor(data: Partial) { + super() + this.assignData(data) } } \ No newline at end of file diff --git a/src/models/user.ts b/src/models/user.ts index 642acff8..cd6423b7 100644 --- a/src/models/user.ts +++ b/src/models/user.ts @@ -13,17 +13,18 @@ export interface IUser extends IAbstract { } export default class UserModel extends AbstractModel implements IUser { - id!: number - email!: string - username!: string - name!: string + id = 0 + email = '' + username = '' + name = '' - created: Date - updated: Date - settings: IUserSettings + created: Date = null + updated: Date = null + settings: IUserSettings = null - constructor(data) { - super(data) + constructor(data: Partial) { + super() + this.assignData(data) this.created = new Date(this.created) this.updated = new Date(this.updated) @@ -33,19 +34,6 @@ export default class UserModel extends AbstractModel implements IUser { } } - defaults() { - return { - id: 0, - email: '', - username: '', - name: '', - - created: null, - updated: null, - settings: null, - } - } - getAvatarUrl(size = 50) { return `${window.API_URL}/avatar/${this.username}?size=${size}` } diff --git a/src/models/userList.ts b/src/models/userList.ts index 51c2a229..ae5f8811 100644 --- a/src/models/userList.ts +++ b/src/models/userList.ts @@ -7,12 +7,10 @@ export interface IUserList extends UserShareBaseModel { // This class extends the user share model with a 'rights' parameter which is used in sharing export default class UserListModel extends UserShareBaseModel implements IUserList { - listId!: IList['id'] + listId: IList['id'] = 0 - defaults() { - return { - ...super.defaults(), - listId: 0, - } + constructor(data: Partial) { + super(data) + this.assignData(data) } } \ No newline at end of file diff --git a/src/models/userNamespace.ts b/src/models/userNamespace.ts index 6d7c8068..4795b873 100644 --- a/src/models/userNamespace.ts +++ b/src/models/userNamespace.ts @@ -7,12 +7,10 @@ export interface IUserNamespace extends UserShareBaseModel { // This class extends the user share model with a 'rights' parameter which is used in sharing export default class UserNamespaceModel extends UserShareBaseModel implements IUserNamespace { - namespaceId!: INamespace['id'] + namespaceId: INamespace['id'] = 0 - defaults() { - return { - ...super.defaults(), - namespaceId: 0, - } + constructor(data: Partial) { + super(data) + this.assignData(data) } } \ No newline at end of file diff --git a/src/models/userSettings.ts b/src/models/userSettings.ts index c5bc638a..382e7159 100644 --- a/src/models/userSettings.ts +++ b/src/models/userSettings.ts @@ -14,25 +14,17 @@ export interface IUserSettings extends IAbstract { } export default class UserSettingsModel extends AbstractModel implements IUserSettings { - name!: string - emailRemindersEnabled!: boolean - discoverableByName!: boolean - discoverableByEmail!: boolean - overdueTasksRemindersEnabled!: boolean - defaultListId!: undefined | IList['id'] - weekStart!: 0 | 1 | 2 | 3 | 4 | 5 | 6 - timezone!: string + name = '' + emailRemindersEnabled = true + discoverableByName = false + discoverableByEmail = false + overdueTasksRemindersEnabled = true + defaultListId: undefined | IList['id'] = undefined + weekStart: 0 | 1 | 2 | 3 | 4 | 5 | 6 = 0 + timezone = '' - defaults() { - return { - name: '', - emailRemindersEnabled: true, - discoverableByName: false, - discoverableByEmail: false, - overdueTasksRemindersEnabled: true, - defaultListId: undefined, - weekStart: 0, - timezone: '', - } + constructor(data: Partial) { + super() + this.assignData(data) } } \ No newline at end of file diff --git a/src/models/userShareBase.ts b/src/models/userShareBase.ts index 27470000..82ab5432 100644 --- a/src/models/userShareBase.ts +++ b/src/models/userShareBase.ts @@ -11,25 +11,17 @@ export interface IUserShareBase extends IAbstract { } export default class UserShareBaseModel extends AbstractModel implements IUserShareBase { - userId!: IUser['id'] - right!: Right + userId: IUser['id'] = '' + right: Right = RIGHTS.READ - created: Date - updated: Date + created: Date = null + updated: Date = null - constructor(data) { - super(data) + constructor(data: Partial) { + super() + this.assignData(data) + this.created = new Date(this.created) this.updated = new Date(this.updated) } - - defaults() { - return { - userId: '', - right: RIGHTS.READ, - - created: null, - updated: null, - } - } } \ No newline at end of file