2018-07-23 16:42:17 +02:00
|
|
|
import { BaseModel } from '../base.model';
|
2018-08-08 17:25:39 +02:00
|
|
|
import { MotionVersion } from './motion-version';
|
|
|
|
import { MotionSubmitter } from './motion-submitter';
|
|
|
|
import { MotionLog } from './motion-log';
|
|
|
|
import { Config } from '../core/config';
|
|
|
|
import { Workflow } from './workflow';
|
2018-08-09 16:03:24 +02:00
|
|
|
import { User } from '../users/user';
|
|
|
|
import { Category } from './category';
|
2018-08-16 17:03:39 +02:00
|
|
|
import { WorkflowState } from './workflow-state';
|
2018-07-04 17:51:31 +02:00
|
|
|
|
2018-07-12 14:11:31 +02:00
|
|
|
/**
|
|
|
|
* Representation of Motion.
|
|
|
|
*
|
2018-08-08 17:25:39 +02:00
|
|
|
* Slightly Defined cause heavy maintaince on server side.
|
2018-07-12 14:11:31 +02:00
|
|
|
*
|
|
|
|
* @ignore
|
|
|
|
*/
|
2018-07-04 17:51:31 +02:00
|
|
|
export class Motion extends BaseModel {
|
2018-07-12 14:11:31 +02:00
|
|
|
protected _collectionString: string;
|
2018-07-04 17:51:31 +02:00
|
|
|
id: number;
|
2018-07-12 14:11:31 +02:00
|
|
|
identifier: string;
|
2018-08-08 17:25:39 +02:00
|
|
|
versions: MotionVersion[];
|
2018-07-04 17:51:31 +02:00
|
|
|
active_version: number;
|
2018-07-12 14:11:31 +02:00
|
|
|
parent_id: number;
|
2018-07-04 17:51:31 +02:00
|
|
|
category_id: number;
|
|
|
|
motion_block_id: number;
|
|
|
|
origin: string;
|
2018-08-08 17:25:39 +02:00
|
|
|
submitters: MotionSubmitter[];
|
2018-07-04 17:51:31 +02:00
|
|
|
supporters_id: number[];
|
2018-07-12 14:11:31 +02:00
|
|
|
comments: Object;
|
|
|
|
state_id: number;
|
|
|
|
state_required_permission_to_see: string;
|
|
|
|
recommendation_id: number;
|
2018-07-04 17:51:31 +02:00
|
|
|
tags_id: number[];
|
2018-07-12 14:11:31 +02:00
|
|
|
attachments_id: number[];
|
|
|
|
polls: BaseModel[];
|
|
|
|
agenda_item_id: number;
|
2018-08-08 17:25:39 +02:00
|
|
|
log_messages: MotionLog[];
|
2018-07-04 17:51:31 +02:00
|
|
|
|
2018-08-23 10:35:05 +02:00
|
|
|
// dynamic values
|
2018-08-20 12:28:43 +02:00
|
|
|
workflow: Workflow;
|
|
|
|
|
2018-08-22 11:26:53 +02:00
|
|
|
// for request
|
|
|
|
title: string;
|
|
|
|
text: string;
|
|
|
|
|
2018-07-04 17:51:31 +02:00
|
|
|
constructor(
|
2018-07-12 14:11:31 +02:00
|
|
|
id?: number,
|
|
|
|
identifier?: string,
|
2018-08-08 17:25:39 +02:00
|
|
|
versions?: MotionVersion[],
|
2018-07-04 17:51:31 +02:00
|
|
|
active_version?: number,
|
2018-07-12 14:11:31 +02:00
|
|
|
parent_id?: number,
|
2018-07-04 17:51:31 +02:00
|
|
|
category_id?: number,
|
|
|
|
motion_block_id?: number,
|
|
|
|
origin?: string,
|
2018-08-08 17:25:39 +02:00
|
|
|
submitters?: MotionSubmitter[],
|
2018-07-04 17:51:31 +02:00
|
|
|
supporters_id?: number[],
|
2018-07-12 14:11:31 +02:00
|
|
|
comments?: Object,
|
|
|
|
state_id?: number,
|
|
|
|
state_required_permission_to_see?: string,
|
|
|
|
recommendation_id?: number,
|
2018-07-04 17:51:31 +02:00
|
|
|
tags_id?: number[],
|
2018-07-12 14:11:31 +02:00
|
|
|
attachments_id?: number[],
|
|
|
|
polls?: BaseModel[],
|
|
|
|
agenda_item_id?: number,
|
2018-08-08 17:25:39 +02:00
|
|
|
log_messages?: MotionLog[]
|
2018-07-04 17:51:31 +02:00
|
|
|
) {
|
2018-07-12 14:11:31 +02:00
|
|
|
super();
|
|
|
|
this._collectionString = 'motions/motion';
|
|
|
|
this.id = id;
|
2018-08-21 14:56:26 +02:00
|
|
|
this.identifier = identifier || '';
|
|
|
|
this.versions = versions || [new MotionVersion()];
|
2018-07-04 17:51:31 +02:00
|
|
|
this.active_version = active_version;
|
2018-07-12 14:11:31 +02:00
|
|
|
this.parent_id = parent_id;
|
2018-07-04 17:51:31 +02:00
|
|
|
this.category_id = category_id;
|
|
|
|
this.motion_block_id = motion_block_id;
|
2018-08-21 14:56:26 +02:00
|
|
|
this.origin = origin || '';
|
2018-08-22 11:26:53 +02:00
|
|
|
this.submitters = submitters || [];
|
2018-07-04 17:51:31 +02:00
|
|
|
this.supporters_id = supporters_id;
|
2018-07-12 14:11:31 +02:00
|
|
|
this.comments = comments;
|
|
|
|
this.state_id = state_id;
|
2018-08-21 14:56:26 +02:00
|
|
|
this.state_required_permission_to_see = state_required_permission_to_see || '';
|
2018-07-12 14:11:31 +02:00
|
|
|
this.recommendation_id = recommendation_id;
|
2018-07-04 17:51:31 +02:00
|
|
|
this.tags_id = tags_id;
|
2018-07-12 14:11:31 +02:00
|
|
|
this.attachments_id = attachments_id;
|
|
|
|
this.polls = polls;
|
|
|
|
this.agenda_item_id = agenda_item_id;
|
2018-08-22 11:26:53 +02:00
|
|
|
this.log_messages = log_messages || [];
|
2018-08-20 12:28:43 +02:00
|
|
|
|
|
|
|
this.initDataStoreValues();
|
|
|
|
}
|
|
|
|
|
2018-08-20 18:13:28 +02:00
|
|
|
/**
|
|
|
|
* update the values of the motion with new values
|
|
|
|
*/
|
|
|
|
patchValues(update: object) {
|
|
|
|
Object.assign(this, update);
|
|
|
|
}
|
|
|
|
|
2018-08-20 12:28:43 +02:00
|
|
|
/**
|
2018-08-23 10:35:05 +02:00
|
|
|
* sets the and the workflow from either dataStore or WebSocket
|
2018-08-20 12:28:43 +02:00
|
|
|
*/
|
|
|
|
initDataStoreValues() {
|
2018-08-23 10:35:05 +02:00
|
|
|
// check the containing Workflows in DataStore
|
|
|
|
const allWorkflows = this.DS.get(Workflow) as Workflow[];
|
|
|
|
allWorkflows.forEach(localWorkflow => {
|
|
|
|
if (localWorkflow.isStateContained(this.state_id)) {
|
|
|
|
this.workflow = localWorkflow as Workflow;
|
|
|
|
}
|
|
|
|
});
|
2018-08-20 12:28:43 +02:00
|
|
|
|
2018-08-23 10:35:05 +02:00
|
|
|
// observe for new models
|
|
|
|
this.DS.getObservable().subscribe(newModel => {
|
|
|
|
if (newModel instanceof Workflow) {
|
|
|
|
if (newModel.isStateContained(this.state_id)) {
|
|
|
|
this.workflow = newModel as Workflow;
|
2018-08-20 12:28:43 +02:00
|
|
|
}
|
2018-08-23 10:35:05 +02:00
|
|
|
}
|
|
|
|
});
|
2018-07-04 17:51:31 +02:00
|
|
|
}
|
2018-08-08 17:25:39 +02:00
|
|
|
|
2018-08-23 10:35:05 +02:00
|
|
|
/**
|
|
|
|
* add a new motionSubmitter from user-object
|
|
|
|
* @param user the user
|
|
|
|
*/
|
2018-08-22 11:26:53 +02:00
|
|
|
addSubmitter(user: User) {
|
|
|
|
const newSubmitter = new MotionSubmitter(null, user.id);
|
|
|
|
this.submitters.push(newSubmitter);
|
|
|
|
console.log('did addSubmitter. this.submitters: ', this.submitters);
|
|
|
|
}
|
|
|
|
|
2018-08-09 16:03:24 +02:00
|
|
|
/**
|
|
|
|
* returns the most current title from versions
|
|
|
|
*/
|
2018-08-20 18:13:28 +02:00
|
|
|
get currentTitle(): string {
|
2018-08-21 14:56:26 +02:00
|
|
|
if (this.versions && this.versions[0]) {
|
2018-08-09 16:03:24 +02:00
|
|
|
return this.versions[0].title;
|
|
|
|
} else {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-23 10:35:05 +02:00
|
|
|
/**
|
|
|
|
* Patch the current version
|
|
|
|
*
|
|
|
|
* TODO: Altering the current version should be avoided.
|
|
|
|
*/
|
2018-08-20 18:13:28 +02:00
|
|
|
set currentTitle(newTitle: string) {
|
|
|
|
if (this.versions[0]) {
|
|
|
|
this.versions[0].title = newTitle;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-09 16:03:24 +02:00
|
|
|
/**
|
|
|
|
* returns the most current motion text from versions
|
|
|
|
*/
|
|
|
|
get currentText() {
|
2018-08-21 14:56:26 +02:00
|
|
|
if (this.versions) {
|
|
|
|
return this.versions[0].text;
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
2018-08-09 16:03:24 +02:00
|
|
|
}
|
|
|
|
|
2018-08-20 18:13:28 +02:00
|
|
|
set currentText(newText: string) {
|
|
|
|
this.versions[0].text = newText;
|
|
|
|
}
|
|
|
|
|
2018-08-09 16:03:24 +02:00
|
|
|
/**
|
|
|
|
* returns the most current motion reason text from versions
|
|
|
|
*/
|
|
|
|
get currentReason() {
|
2018-08-21 14:56:26 +02:00
|
|
|
if (this.versions) {
|
|
|
|
return this.versions[0].reason;
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
2018-08-09 16:03:24 +02:00
|
|
|
}
|
|
|
|
|
2018-08-21 14:56:26 +02:00
|
|
|
/**
|
|
|
|
* Update the current reason.
|
|
|
|
* TODO: ignores motion versions. Should make a new one.
|
|
|
|
*/
|
2018-08-20 18:13:28 +02:00
|
|
|
set currentReason(newReason: string) {
|
|
|
|
this.versions[0].reason = newReason;
|
|
|
|
}
|
|
|
|
|
2018-08-08 17:25:39 +02:00
|
|
|
/**
|
|
|
|
* return the submitters as uses objects
|
|
|
|
*/
|
|
|
|
get submitterAsUser() {
|
|
|
|
const submitterIds = [];
|
2018-08-22 11:26:53 +02:00
|
|
|
if (this.submitters && this.submitters.length > 0) {
|
2018-08-21 14:56:26 +02:00
|
|
|
this.submitters.forEach(submitter => {
|
|
|
|
submitterIds.push(submitter.user_id);
|
|
|
|
});
|
|
|
|
const users = this.DS.get(User, ...submitterIds);
|
|
|
|
return users;
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
2018-08-08 17:25:39 +02:00
|
|
|
}
|
|
|
|
|
2018-08-09 16:03:24 +02:00
|
|
|
/**
|
|
|
|
* get the category of a motion as object
|
|
|
|
*/
|
2018-08-16 17:03:39 +02:00
|
|
|
get category(): any {
|
2018-08-09 16:03:24 +02:00
|
|
|
if (this.category_id) {
|
|
|
|
const motionCategory = this.DS.get(Category, this.category_id);
|
2018-08-16 17:03:39 +02:00
|
|
|
return motionCategory as Category;
|
2018-08-09 16:03:24 +02:00
|
|
|
} else {
|
2018-08-21 14:56:26 +02:00
|
|
|
return '';
|
2018-08-09 16:03:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-08 17:25:39 +02:00
|
|
|
/**
|
2018-08-16 17:03:39 +02:00
|
|
|
* Set the category in the motion
|
|
|
|
*/
|
|
|
|
set category(newCategory: any) {
|
|
|
|
this.category_id = newCategory.id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* return the workflow state
|
|
|
|
*/
|
2018-08-21 14:56:26 +02:00
|
|
|
get state(): any {
|
2018-08-23 10:35:05 +02:00
|
|
|
if (this.workflow) {
|
|
|
|
return this.workflow.state_by_id(this.state_id);
|
2018-08-20 12:28:43 +02:00
|
|
|
} else {
|
2018-08-21 14:56:26 +02:00
|
|
|
return '';
|
2018-08-20 12:28:43 +02:00
|
|
|
}
|
2018-08-14 12:55:45 +02:00
|
|
|
}
|
|
|
|
|
2018-08-16 17:03:39 +02:00
|
|
|
/**
|
|
|
|
* returns possible states for the motion
|
|
|
|
*/
|
2018-08-23 10:35:05 +02:00
|
|
|
get nextStates(): WorkflowState[] {
|
|
|
|
if (this.workflow && this.state) {
|
|
|
|
return this.state.getNextStates(this.workflow);
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
2018-08-08 17:25:39 +02:00
|
|
|
}
|
|
|
|
|
2018-08-13 13:49:38 +02:00
|
|
|
/**
|
|
|
|
* Returns the name of the recommendation.
|
|
|
|
*
|
|
|
|
* TODO: Motion workflow needs to be specific on the server
|
|
|
|
*/
|
2018-08-21 14:56:26 +02:00
|
|
|
get recommendation(): any {
|
|
|
|
if (this.recommendation_id && this.workflow && this.workflow.id) {
|
2018-08-20 18:13:28 +02:00
|
|
|
const state = this.workflow.state_by_id(this.recommendation_id);
|
|
|
|
return state;
|
2018-08-13 13:49:38 +02:00
|
|
|
} else {
|
2018-08-21 14:56:26 +02:00
|
|
|
return '';
|
2018-08-13 13:49:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* returns the value of 'config.motions_recommendations_by'
|
|
|
|
*/
|
|
|
|
get recomBy() {
|
|
|
|
const motionsRecommendationsByConfig = this.DS.filter(
|
|
|
|
Config,
|
|
|
|
config => config.key === 'motions_recommendations_by'
|
|
|
|
)[0] as Config;
|
2018-08-21 14:56:26 +02:00
|
|
|
|
|
|
|
if (motionsRecommendationsByConfig) {
|
|
|
|
const recomByString = motionsRecommendationsByConfig.value;
|
|
|
|
return recomByString;
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
2018-08-13 13:49:38 +02:00
|
|
|
}
|
|
|
|
|
2018-08-08 17:25:39 +02:00
|
|
|
deserialize(input: any): this {
|
|
|
|
Object.assign(this, input);
|
|
|
|
|
|
|
|
if (input.versions instanceof Array) {
|
|
|
|
this.versions = [];
|
|
|
|
input.versions.forEach(motionVersionData => {
|
|
|
|
this.versions.push(new MotionVersion().deserialize(motionVersionData));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (input.submitters instanceof Array) {
|
|
|
|
this.submitters = [];
|
|
|
|
input.submitters.forEach(SubmitterData => {
|
|
|
|
this.submitters.push(new MotionSubmitter().deserialize(SubmitterData));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (input.log_messages instanceof Array) {
|
|
|
|
this.log_messages = [];
|
|
|
|
input.log_messages.forEach(logData => {
|
|
|
|
this.log_messages.push(new MotionLog().deserialize(logData));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
2018-07-04 17:51:31 +02:00
|
|
|
}
|
2018-08-24 13:05:03 +02:00
|
|
|
|
|
|
|
BaseModel.registerCollectionElement('motions/motion', Motion);
|