Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Feature/default dates for children (#8605)

ulferts05c9e3fca6ec
diff --git a/app/services/work_packages/set_attributes_service.rb b/app/services/work_packages/set_attributes_service.rb
index 6fc42e53c0b..bc987255c21 100644
--- a/app/services/work_packages/set_attributes_service.rb
+++ b/app/services/work_packages/set_attributes_service.rb
@@ -59,18 +59,48 @@ class WorkPackages::SetAttributesService < ::BaseServices::SetAttributes
work_package.attributes = assignable_attributes
end
- def set_default_attributes(*)
+ def set_default_attributes(attributes)
return unless work_package.new_record?
- work_package.priority ||= IssuePriority.active.default
+ set_default_priority
+ set_default_author
+ set_default_status
+ set_default_start_date(attributes)
+ set_default_due_date(attributes)
+ end
+
+ def non_or_default_description?
+ work_package.description.blank? || false
+ end
+
+ def set_default_author
work_package.author ||= user
+ end
+
+ def set_default_status
work_package.status ||= Status.default
+ end
- work_package.start_date ||= Date.today if Setting.work_package_startdate_is_adddate?
+ def set_default_priority
+ work_package.priority ||= IssuePriority.active.default
end
- def non_or_default_description?
- work_package.description.blank? || false
+ def set_default_start_date(attributes)
+ work_package.start_date ||= if attributes.has_key?(:start_date)
+ nil
+ elsif parent_start_earlier_than_due?
+ work_package.parent.start_date
+ elsif Setting.work_package_startdate_is_adddate?
+ Date.today
+ end
+ end
+
+ def set_default_due_date(attributes)
+ work_package.due_date ||= if attributes.has_key?(:due_date)
+ nil
+ elsif parent_due_later_than_start?
+ work_package.parent.due_date
+ end
end
def set_templated_description
@@ -249,4 +279,12 @@ class WorkPackages::SetAttributesService < ::BaseServices::SetAttributes
def max_child_date
(work_package.children.map(&:start_date) + work_package.children.map(&:due_date)).compact.max
end
+
+ def parent_start_earlier_than_due?
+ work_package.parent&.start_date && work_package.parent.start_date < (work_package.due_date || work_package.parent.due_date)
+ end
+
+ def parent_due_later_than_start?
+ work_package.parent&.due_date && work_package.parent.due_date > (work_package.start_date || work_package.parent.start_date)
+ end
end
diff --git a/frontend/src/app/components/wp-edit-form/work-package-filter-values.spec.ts b/frontend/src/app/components/wp-edit-form/work-package-filter-values.spec.ts
index c548f7b6776..c1727a252cb 100644
--- a/frontend/src/app/components/wp-edit-form/work-package-filter-values.spec.ts
+++ b/frontend/src/app/components/wp-edit-form/work-package-filter-values.spec.ts
@@ -120,7 +120,7 @@ describe('WorkPackageFilterValues', () => {
}
];
- subject = new WorkPackageFilterValues(injector, changeset, filters);
+ subject = new WorkPackageFilterValues(injector, filters);
}
describe('when a filter value already exists in values', () => {
@@ -141,7 +141,7 @@ describe('WorkPackageFilterValues', () => {
});
it('it should not apply the first value (Regression #30817)', (() => {
- subject.applyDefaultsFromFilters();
+ subject.applyDefaultsFromFilters(changeset);
expect(changeset.changedAttributes.length).toEqual(0);
expect(changeset.value('type').href).toEqual('/api/v3/types/1');
@@ -164,7 +164,7 @@ describe('WorkPackageFilterValues', () => {
});
it('it should not apply the first value (Regression #30817)', (() => {
- subject.applyDefaultsFromFilters();
+ subject.applyDefaultsFromFilters(changeset);
expect(changeset.changedAttributes.length).toEqual(0);
expect(changeset.value('type').href).toEqual('/api/v3/types/2');
diff --git a/frontend/src/app/components/wp-edit-form/work-package-filter-values.ts b/frontend/src/app/components/wp-edit-form/work-package-filter-values.ts
index 30a40521841..7c36dd5cbc6 100644
--- a/frontend/src/app/components/wp-edit-form/work-package-filter-values.ts
+++ b/frontend/src/app/components/wp-edit-form/work-package-filter-values.ts
@@ -7,7 +7,7 @@ import {AngularTrackingHelpers} from "core-components/angular/tracking-functions
import {WorkPackageChangeset} from "core-components/wp-edit/work-package-changeset";
import compareByHrefOrString = AngularTrackingHelpers.compareByHrefOrString;
import {InjectField} from "core-app/helpers/angular/inject-field.decorator";
-import {ApiV3Filter, FilterOperator} from "core-components/api/api-v3/api-v3-filter-builder";
+import {FilterOperator} from "core-components/api/api-v3/api-v3-filter-builder";
export class WorkPackageFilterValues {
@@ -20,13 +20,12 @@ export class WorkPackageFilterValues {
};
constructor(public injector:Injector,
- private change:WorkPackageChangeset,
private filters:QueryFilterInstanceResource[],
private excluded:string[] = []) {
}
- public applyDefaultsFromFilters() {
+ public applyDefaultsFromFilters(change:WorkPackageChangeset|Object) {
_.each(this.filters, filter => {
// Exclude filters specified in constructor
if (this.excluded.indexOf(filter.id) !== -1) {
@@ -38,7 +37,7 @@ export class WorkPackageFilterValues {
const handler = this.handlers[operator];
// Apply the filter if there is any
- handler?.call(this, filter);
+ handler?.call(this, change, filter);
});
}
@@ -48,10 +47,10 @@ export class WorkPackageFilterValues {
* @param filter A positive '=' filter with at least one value
* @private
*/
- private applyFirstValue(filter:QueryFilterInstanceResource):void {
+ private applyFirstValue(change:WorkPackageChangeset|{[id:string]:any}, filter:QueryFilterInstanceResource):void {
// Avoid setting a value if current value is in filter list
// and more than one value selected
- if (this.filterAlreadyApplied(filter)) {
+ if (this.filterAlreadyApplied(change, filter)) {
return;
}
@@ -61,7 +60,7 @@ export class WorkPackageFilterValues {
// Avoid empty values
if (value) {
let attributeName = this.mapFilterToAttribute(filter);
- this.setValueFor(attributeName, value);
+ this.setValueFor(change, attributeName, value);
}
}
@@ -71,16 +70,25 @@ export class WorkPackageFilterValues {
* @param filter A none '!*' filter
* @private
*/
- private setToNull(filter:QueryFilterInstanceResource):void {
+ private setToNull(change:WorkPackageChangeset|{[id:string]:any}, filter:QueryFilterInstanceResource):void {
let attributeName = this.mapFilterToAttribute(filter);
- this.change.setValue(attributeName, { href: null });
+
+ this.setValue(change, attributeName,{ href: null });
}
- private setValueFor(field:string, value:string|HalResource) {
+ private setValueFor(change:WorkPackageChangeset|Object, field:string, value:string|HalResource) {
let newValue = this.findSpecialValue(value, field) || value;
if (newValue) {
- this.change.setValue(field, newValue);
+ this.setValue(change, field, newValue);
+ }
+ }
+
+ private setValue(change:WorkPackageChangeset|{[id:string]:any}, field:string, value:any) {
+ if (change instanceof WorkPackageChangeset) {
+ change.setValue(field, value);
+ } else {
+ change[field] = value;
}
}
@@ -107,13 +115,13 @@ export class WorkPackageFilterValues {
* - changeset already matches one of the selected values
* @param filter
*/
- private filterAlreadyApplied(filter:any):boolean {
+ private filterAlreadyApplied(change:WorkPackageChangeset|{[id:string]:any}, filter:any):boolean {
// Only applicable if more than one selected
if (filter.values.length <= 1) {
return false;
}
- const current = this.change.projectedResource[filter.id];
+ const current = change instanceof WorkPackageChangeset ? change.projectedResource[filter.id] : change[filter.id];
for (let i = 0; i < filter.values.length; i++) {
if (compareByHrefOrString(current, filter.values[i])) {
diff --git a/frontend/src/app/components/wp-new/wp-create.component.ts b/frontend/src/app/components/wp-new/wp-create.component.ts
index 3834d431a26..0d8ccf888f9 100644
--- a/frontend/src/app/components/wp-new/wp-create.component.ts
+++ b/frontend/src/app/components/wp-new/wp-create.component.ts
@@ -45,6 +45,8 @@ import * as URI from 'urijs';
import {UntilDestroyedMixin} from "core-app/helpers/angular/until-destroyed.mixin";
import {splitViewRoute} from "core-app/modules/work_packages/routing/split-view-routes.helper";
import {APIV3Service} from "core-app/modules/apiv3/api-v3.service";
+import {HalSource, HalSourceLinks} from "core-app/modules/hal/resources/hal-resource";
+import {HalLinkSource} from "core-app/modules/hal/hal-link/hal-link";
@Directive()
export class WorkPackageCreateComponent extends UntilDestroyedMixin implements OnInit {
@@ -169,10 +171,22 @@ export class WorkPackageCreateComponent extends UntilDestroyedMixin implements O
}
protected createdWorkPackage() {
+ let defaults:HalSource = {
+ _links: {}
+ };
+
const type = this.stateParams.type ? parseInt(this.stateParams.type) : undefined;
+ const parent = this.stateParams.parent_id ? parseInt(this.stateParams.parent_id) : undefined;
const project = this.stateParams.projectPath;
- return this.wpCreate.createOrContinueWorkPackage(project, type);
+ if (type) {
+ defaults._links['type'] = { href: this.apiV3Service.types.id(type).path };
+ }
+ if (parent) {
+ defaults._links['parent'] = { href: this.apiV3Service.work_packages.id(parent).path };
+ }
+
+ return this.wpCreate.createOrContinueWorkPackage(project, type, defaults);
}
private closeEditFormWhenNewWorkPackageSaved() {
diff --git a/frontend/src/app/components/wp-new/wp-create.service.ts b/frontend/src/app/components/wp-new/wp-create.service.ts
index dfe45cb03f9..1c1ef193b22 100644
--- a/frontend/src/app/components/wp-new/wp-create.service.ts
+++ b/frontend/src/app/components/wp-new/wp-create.service.ts
@@ -32,7 +32,6 @@ import {WorkPackageResource} from 'core-app/modules/hal/resources/work-package-r
import {HalResourceService} from 'core-app/modules/hal/services/hal-resource.service';
import {HookService} from 'core-app/modules/plugins/hook-service';
import {WorkPackageFilterValues} from "core-components/wp-edit-form/work-package-filter-values";
-
import {
HalResourceEditingService,
ResourceChangesetCommit
@@ -46,7 +45,8 @@ import {AuthorisationService} from "core-app/modules/common/model-auth/model-aut
import {UntilDestroyedMixin} from "core-app/helpers/angular/until-destroyed.mixin";
import {SchemaCacheService} from "core-components/schemas/schema-cache.service";
import {APIV3Service} from "core-app/modules/apiv3/api-v3.service";
-import {Form} from "@angular/forms";
+import {HalResource, HalSource, HalSourceLink} from "core-app/modules/hal/resources/hal-resource";
+import {SchemaResource} from "core-app/modules/hal/resources/schema-resource";
export const newWorkPackageHref = '/api/v3/work_packages/new';
@@ -98,23 +98,17 @@ export class WorkPackageCreateService extends UntilDestroyedMixin {
return this.newWorkPackageCreatedSubject.asObservable();
}
- public createNewWorkPackage(projectIdentifier:string|undefined|null):Promise<WorkPackageChangeset> {
- return this.getEmptyForm(projectIdentifier).then(form => {
- return this.fromCreateForm(form);
- });
- }
-
- public createNewTypedWorkPackage(projectIdentifier:string|undefined|null, type:number):Promise<WorkPackageChangeset> {
+ public createNewWorkPackage(projectIdentifier:string|undefined|null, payload:HalSource):Promise<WorkPackageChangeset> {
return this
.apiV3Service
.withOptionalProject(projectIdentifier)
.work_packages
.form
- .forType(type)
+ .forPayload(payload)
.toPromise()
.then((form:FormResource) => {
- return this.fromCreateForm(form);
- });
+ return this.fromCreateForm(form);
+ });
}
public fromCreateForm(form:FormResource):WorkPackageChangeset {
@@ -184,11 +178,11 @@ export class WorkPackageCreateService extends UntilDestroyedMixin {
.values$();
}
- public createOrContinueWorkPackage(projectIdentifier:string|null|undefined, type?:number) {
+ public createOrContinueWorkPackage(projectIdentifier:string|null|undefined, type?:number, defaults?:HalSource) {
let changePromise = this.continueExistingEdit(type);
if (!changePromise) {
- changePromise = this.createNewWithDefaults(projectIdentifier, type);
+ changePromise = this.createNewWithDefaults(projectIdentifier, defaults);
}
return changePromise.then((change:WorkPackageChangeset) => {
@@ -230,50 +224,117 @@ export class WorkPackageCreateService extends UntilDestroyedMixin {
return null;
}
- protected createNewWithDefaults(projectIdentifier:string|null|undefined, type?:number) {
- let changePromise = null;
-
- if (type) {
- changePromise = this.createNewTypedWorkPackage(projectIdentifier, type);
- } else {
- changePromise = this.createNewWorkPackage(projectIdentifier);
- }
-
- return changePromise.then((change:WorkPackageChangeset) => {
- if (!change) {
- throw 'No new work package was created';
- }
-
- let except:string[] = [];
-
- if (type) {
- except = ['type'];
- }
-
- this.applyDefaults(change, change.projectedResource, except);
-
- return change;
+ /**
+ * Initializes a new work package. The work package is not yet persisted.
+ * The properties of the work package are initialized from two sources:
+ * * The default values provided
+ * * The filter values that might exist in the query space
+ *
+ * The first can be employed to e.g. provide the type or the parent of the work package.
+ * The later can be employed to create a work package that adheres to the filter values.
+ *
+ * @params projectIdentifier The project the work package is to be created in.
+ * @param defaults Values the new work package should possess on creation.
+ */
+ protected createNewWithDefaults(projectIdentifier:string|null|undefined, defaults?:HalSource) {
+ return this
+ .withFiltersPayload(projectIdentifier, defaults)
+ .then(filterDefaults => {
+ const mergedPayload = _.merge({ _links: {} }, filterDefaults, defaults);
+
+ return this.createNewWorkPackage(projectIdentifier, mergedPayload).then((change:WorkPackageChangeset) => {
+ if (!change) {
+ throw 'No new work package was created';
+ }
+
+ // We need to apply the defaults again (after them being applied in the form requests)
+ // here as the initial form requests might have led to some default
+ // values not being carried over. This can happen when custom fields not available in one type are filter values.
+ this.defaultsFromFilters(change, defaults);
+
+ return change;
+ });
});
}
/**
- * Apply values to the work package from the current set of filters
+ * Fetches all values of filters applicable to work as default values (e.g. assignee = 123).
+ * If defaults already contain the type, that filter is ignored.
+ *
+ * The ignoring functionality could be generalized.
*
- * @param changeset
- * @param wp
- * @param except
+ * @params object
+ * @param defaults
*/
- private applyDefaults(change:WorkPackageChangeset, wp:WorkPackageResource, except:string[]) {
+ private defaultsFromFilters(object:HalSource|WorkPackageChangeset, defaults?:HalSource) {
// Not using WorkPackageViewFiltersService here as the embedded table does not load the form
// which will result in that service having empty current filters.
let query = this.querySpace.query.value;
if (query) {
- const filter = new WorkPackageFilterValues(this.injector, change, query.filters, except);
- filter.applyDefaultsFromFilters();
+ const except:string[] = defaults?._links && defaults._links['type'] ? ['type'] : [];
+
+ new WorkPackageFilterValues(this.injector, query.filters, except)
+ .applyDefaultsFromFilters(object);
}
}
+ /**
+ * Returns valid payload based on the filters active in the query space validated by the backend via a form
+ * request. In case no filters are active, the (empty) filters payload is just passed through.
+ *
+ * If there are filters applied, we need the additional form request to turn the defaults of the filters into
+ * a valid payload in the sense that all properties are at their correct place and are in the right format. That means
+ * HalResources are in the _links section and follow the { href: some_link } format while simple properties stay on the
+ * top level.
+ */
+ private withFiltersPayload(projectIdentifier:string|null|undefined, defaults?:HalSource):Promise<HalSource> {
+ const fromFilter = { _links: {} };
+ this.defaultsFromFilters(fromFilter, defaults);
+
+ const filtersApplied = Object.keys(fromFilter).length > 1 || Object.keys(fromFilter._links).length > 0;
+
+ if (filtersApplied) {
+ return this
+ .apiV3Service
+ .withOptionalProject(projectIdentifier)
+ .work_packages
+ .form
+ .forTypePayload(defaults || { _links: {} })
+ .toPromise()
+ .then((form:FormResource) => {
+ this.toApiPayload(fromFilter, form.schema);
+ return fromFilter;
+ });
+ } else {
+ return Promise.resolve(fromFilter);
+ }
+ }
+
+ private toApiPayload(payload:HalSource, schema:SchemaResource) {
+ let links:string[] = [];
+
+ Object.keys(schema.$source).forEach(attribute => {
+ if (!['Integer', 'Float', 'Date', 'DateTime', 'Duration', 'Formattable', 'Boolean', undefined].includes(schema.$source[attribute].type)) {
+ links.push(attribute);
+ }
+ });
+
+ links.forEach(attribute => {
+ const value = payload[attribute];
+ if (value === undefined) {
+ // nothing
+ } else if (value instanceof HalResource) {
+ payload._links[attribute] = { href: value.$links.self.href };
+ } else if (!value) {
+ payload._links[attribute] = { href: null };
+ } else {
+ payload._links[attribute] = value as unknown as HalSourceLink;
+ }
+ delete payload[attribute];
+ });
+ }
+
/**
* Assign values from the form for a newly created work package resource.
* @param form
diff --git a/frontend/src/app/modules/apiv3/endpoints/work_packages/api-v3-work-package-cached-subresource.ts b/frontend/src/app/modules/apiv3/endpoints/work_packages/api-v3-work-package-cached-subresource.ts
index b5e6f51627e..a35888745fa 100644
--- a/frontend/src/app/modules/apiv3/endpoints/work_packages/api-v3-work-package-cached-subresource.ts
+++ b/frontend/src/app/modules/apiv3/endpoints/work_packages/api-v3-work-package-cached-subresource.ts
@@ -26,15 +26,10 @@
// See docs/COPYRIGHT.rdoc for more details.
// ++
-import {APIv3GettableResource, APIv3ResourcePath} from "core-app/modules/apiv3/paths/apiv3-resource";
-import {WorkPackageResource} from "core-app/modules/hal/resources/work-package-resource";
-import {Apiv3RelationsPaths} from "core-app/modules/apiv3/endpoints/relations/apiv3-relations-paths";
+import {APIv3GettableResource} from "core-app/modules/apiv3/paths/apiv3-resource";
import {WorkPackageCollectionResource} from "core-app/modules/hal/resources/wp-collection-resource";
import {Observable} from "rxjs";
-import {ApiV3FilterBuilder} from "core-components/api/api-v3/api-v3-filter-builder";
-import {CachableAPIV3Resource} from "core-app/modules/apiv3/cache/cachable-apiv3-resource";
import {APIV3WorkPackagesPaths} from "core-app/modules/apiv3/endpoints/work_packages/api-v3-work-packages-paths";
-import {StateCacheService} from "core-app/modules/apiv3/cache/state-cache.service";
import {take, tap} from "rxjs/operators";
import {WorkPackageCache} from "core-app/modules/apiv3/endpoints/work_packages/work-package.cache";
diff --git a/frontend/src/app/modules/apiv3/endpoints/work_packages/apiv3-work-package-form.ts b/frontend/src/app/modules/apiv3/endpoints/work_packages/apiv3-work-package-form.ts
index b5bb7534d1f..6633d67100d 100644
--- a/frontend/src/app/modules/apiv3/endpoints/work_packages/apiv3-work-package-form.ts
+++ b/frontend/src/app/modules/apiv3/endpoints/work_packages/apiv3-work-package-form.ts
@@ -1,21 +1,30 @@
import {APIv3FormResource} from "core-app/modules/apiv3/forms/apiv3-form-resource";
import {FormResource} from "core-app/modules/hal/resources/form-resource";
import {Observable} from "rxjs";
+import {HalSource} from "core-app/modules/hal/resources/hal-resource";
export class APIv3WorkPackageForm extends APIv3FormResource {
/**
- * Returns a promise to post `/api/v3/work_packages/form` where the
- * type has already been set to the one provided.
+ * Returns a promise to post `/api/v3/work_packages/form` with only the type part of the
+ * provided payload being sent to the backend.
*
- * @param typeId: The id of the type to initialize the form with
- * @returns An empty work package form resource.
+ * @param payload: The payload to be sent to the backend
+ * @returns A work package form resource prefilled with the provided payload.
*/
- public forType(typeId:number):Observable<FormResource> {
-
- const typeUrl = this.apiRoot.types.id(typeId).path;
- const request = { _links: { type: { href: typeUrl } } };
+ public forTypePayload(payload:HalSource):Observable<FormResource> {
+ let typePayload = payload._links['type'] ? { _links: { type: payload['_links']['type'] } } : { _links: {} } ;
- return this.post(request);
+ return this.post(payload);
+ }
+ /**
+ * Returns a promise to post `/api/v3/work_packages/form` where the
+ * payload sent to the backend has been provided.
+ *
+ * @param payload: The payload to be sent to the backend
+ * @returns A work package form resource prefilled with the provided payload.
+ */
+ public forPayload(payload:HalSource):Observable<FormResource> {
+ return this.post(payload);
}
}
diff --git a/frontend/src/app/modules/boards/board/board-actions/board-action.service.ts b/frontend/src/app/modules/boards/board/board-actions/board-action.service.ts
index 753c0b80d96..41e76950aa4 100644
--- a/frontend/src/app/modules/boards/board/board-actions/board-action.service.ts
+++ b/frontend/src/app/modules/boards/board/board-actions/board-action.service.ts
@@ -230,8 +230,8 @@ export abstract class BoardActionService {
);
}
- const filter = new WorkPackageFilterValues(this.injector, changeset, query.filters);
- filter.applyDefaultsFromFilters();
+ new WorkPackageFilterValues(this.injector, query.filters)
+ .applyDefaultsFromFilters(changeset);
}
/**
diff --git a/frontend/src/app/modules/boards/board/board-list/board-list.component.ts b/frontend/src/app/modules/boards/board/board-list/board-list.component.ts
index 2d2901af424..85c60dde3a1 100644
--- a/frontend/src/app/modules/boards/board/board-list/board-list.component.ts
+++ b/frontend/src/app/modules/boards/board/board-list/board-list.component.ts
@@ -31,8 +31,6 @@ import {WorkPackageStatesInitializationService} from "core-components/wp-list/wp
import {ApiV3Filter} from "core-components/api/api-v3/api-v3-filter-builder";
import {BoardService} from "app/modules/boards/board/board.service";
import {WorkPackageResource} from "core-app/modules/hal/resources/work-package-resource";
-import {WorkPackageFilterValues} from "core-components/wp-edit-form/work-package-filter-values";
-
import {HalResourceEditingService} from "core-app/modules/fields/edit/services/hal-resource-editing.service";
import {HalResourceNotificationService} from "core-app/modules/hal/services/hal-resource-notification.service";
import {BoardActionsRegistryService} from "core-app/modules/boards/board/board-actions/board-actions-registry.service";
diff --git a/frontend/src/app/modules/common/path-helper/path-helper.service.ts b/frontend/src/app/modules/common/path-helper/path-helper.service.ts
index 08371485be1..10e2e553cf3 100644
--- a/frontend/src/app/modules/common/path-helper/path-helper.service.ts
+++ b/frontend/src/app/modules/common/path-helper/path-helper.service.ts
@@ -81,7 +81,6 @@ class Apiv3Paths {
'/principals?' +
filters.toParams({ sortBy: '[["name","asc"]]', offset: '1', pageSize: '10' });
}
-
}
@Injectable({ providedIn: 'root' })
diff --git a/frontend/src/app/modules/fields/edit/field-types/combined-date-edit-field.component.ts b/frontend/src/app/modules/fields/edit/field-types/combined-date-edit-field.component.ts
index 9b5f6750bb9..85dd54f5012 100644
--- a/frontend/src/app/modules/fields/edit/field-types/combined-date-edit-field.component.ts
+++ b/frontend/src/app/modules/fields/edit/field-types/combined-date-edit-field.component.ts
@@ -97,4 +97,13 @@ export class CombinedDateEditFieldComponent extends DateEditFieldComponent imple
this.handler.handleUserSubmit();
});
}
+
+ // Overwrite super in order to set the inital dates.
+ protected initialize() {
+ super.initialize();
+
+ // this breaks the preceived abstraction of the edit fields. But the date picker
+ // is already highly specific to start and due Date.
+ this.dates = `${this.resource['startDate']} - ${this.resource['dueDate']}`;
+ }
}
diff --git a/frontend/src/app/modules/hal/resources/hal-resource.ts b/frontend/src/app/modules/hal/resources/hal-resource.ts
index 3cdd5e41c60..3ab899a2e0f 100644
--- a/frontend/src/app/modules/hal/resources/hal-resource.ts
+++ b/frontend/src/app/modules/hal/resources/hal-resource.ts
@@ -41,6 +41,17 @@ export interface HalResourceClass<T extends HalResource = HalResource> {
$halType:string):T;
}
+export type HalSourceLink = { href:string|null };
+
+export type HalSourceLinks = {
+ [key:string]:HalSourceLink
+};
+
+export type HalSource = {
+ [key:string]:string|number|null|HalSourceLinks,
+ _links:HalSourceLinks
+};
+
export class HalResource {
// TODO this is the source of many issues in the frontend
// because it no longer properly type checks stuff
diff --git a/frontend/src/app/modules/hal/schemas/hal-payload.helper.ts b/frontend/src/app/modules/hal/schemas/hal-payload.helper.ts
index e2bbf3c5fcb..a8790927751 100644
--- a/frontend/src/app/modules/hal/schemas/hal-payload.helper.ts
+++ b/frontend/src/app/modules/hal/schemas/hal-payload.helper.ts
@@ -85,7 +85,7 @@ export class HalPayloadHelper {
_.each(nonLinkProperties, property => {
if (resource.hasOwnProperty(property) || resource[property]) {
if (Array.isArray(resource[property])) {
- payload[property] = _.map(resource[property], (element:any) => {
+ payload[property] = _.map(resource[property], (element:any) => {
if (element instanceof HalResource) {
return this.extractPayloadFromSchema(element, element.currentSchema || element.schema);
} else {
@@ -93,7 +93,7 @@ export class HalPayloadHelper {
}
});
} else {
- payload[property] = resource[property];
+ payload[property] = resource[property];
}
}
});
diff --git a/spec/features/work_packages/new/new_work_package_spec.rb b/spec/features/work_packages/new/new_work_package_spec.rb
index 063e06e0952..a4c37f256b5 100644
--- a/spec/features/work_packages/new/new_work_package_spec.rb
+++ b/spec/features/work_packages/new/new_work_package_spec.rb
@@ -13,7 +13,7 @@ describe 'new work package', js: true do
FactoryBot.create(:project, types: types)
end
- let(:permissions) { [:view_work_packages, :add_work_packages, :edit_work_packages] }
+ let(:permissions) { %i[view_work_packages add_work_packages edit_work_packages] }
let(:user) do
FactoryBot.create(:user,
member_in_project: project,
@@ -403,4 +403,79 @@ describe 'new work package', js: true do
end
end
end
+
+ context 'creating child work packages' do
+ let!(:parent) do
+ FactoryBot.create(:work_package,
+ project: project,
+ author: user,
+ start_date: Date.today - 5.days,
+ due_date: Date.today + 5.days)
+ end
+ let(:context_menu) { Components::WorkPackages::ContextMenu.new }
+ let(:split_create_page) { Pages::SplitWorkPackageCreate.new(project: project) }
+ let(:permissions) { %i[view_work_packages add_work_packages edit_work_packages manage_subtasks] }
+ let(:wp_page) { Pages::FullWorkPackage.new(parent) }
+ let(:wp_page_create) { Pages::FullWorkPackageCreate.new(project: project) }
+
+ it 'from within the table' do
+ work_packages_page.visit_index
+
+ context_menu.open_for(parent)
+ context_menu.choose('Create new child')
+
+ # The dates are taken over from the parent by default
+ date_field = split_create_page.edit_field(:combinedDate)
+ date_field.expect_value("#{parent.start_date} - #{parent.due_date}" )
+
+ date_field.input_element.click
+ sleep 1
+ date_field.clear with_backspace: true
+ date_field.input_element.send_keys :backspace
+
+ date_field.save!
+
+ subject = split_create_page.edit_field(:subject)
+ subject.set_value 'Child'
+
+ split_create_page.save!
+
+ split_create_page.expect_and_dismiss_notification(message: I18n.t('js.notice_successful_create'))
+
+ split_create_page.expect_attributes(combinedDate: "no start date - #{parent.due_date.strftime('%m/%d/%Y')}")
+
+ expect(split_create_page).to have_selector('.wp-breadcrumb', text: "Parent:\n#{parent.subject}")
+ end
+
+ it 'from the relations tab' do
+ wp_page.visit_tab!('relations')
+
+ click_link('Create new child')
+
+ ## The dates are taken over from the parent by default
+ #date_field = wp_page_create.edit_field(:combinedDate)
+ #date_field.expect_value("#{parent.start_date} - #{parent.due_date}" )
+
+ #date_field.input_element.click
+ #sleep 1
+ #date_field.clear with_backspace: true
+ #date_field.input_element.send_keys :backspace
+
+ #date_field.save!
+
+ subject = EditField.new wp_page, :subject
+ subject.set_value 'Child'
+ subject.submit_by_enter
+
+ wp_page.expect_and_dismiss_notification(message: I18n.t('js.notice_successful_create'))
+ sleep 1
+
+ # Move to the newly created child
+ wp_page.find("wp-children-query tbody.results-tbody tr").double_click
+
+ wp_page.expect_attributes(combinedDate: "#{parent.start_date.strftime('%m/%d/%Y')} - #{parent.due_date.strftime('%m/%d/%Y')}")
+
+ expect(wp_page).to have_selector('.wp-breadcrumb', text: "Parent:\n#{parent.subject}")
+ end
+ end
end
diff --git a/spec/services/work_packages/set_attributes_service_spec.rb b/spec/services/work_packages/set_attributes_service_spec.rb
index 120b626e2a6..12b64d823c6 100644
--- a/spec/services/work_packages/set_attributes_service_spec.rb
+++ b/spec/services/work_packages/set_attributes_service_spec.rb
@@ -294,51 +294,212 @@ describe WorkPackages::SetAttributesService, type: :model do
end
end
- context 'priority' do
- let(:default_priority) { FactoryBot.build_stubbed(:priority) }
- let(:other_priority) { FactoryBot.build_stubbed(:priority) }
-
- before do
- allow(IssuePriority)
- .to receive_message_chain(:active, :default)
- .and_return(default_priority)
+ context 'start_date & due_date' do
+ let(:parent) do
+ FactoryBot.build_stubbed(:stubbed_work_package,
+ start_date: parent_start_date,
+ due_date: parent_due_date)
end
+ let(:parent_start_date) { Date.today - 5.days }
+ let(:parent_due_date) { Date.today + 10.days }
- context 'no value set before for a new work package' do
- let(:call_attributes) { {} }
+ context 'with a parent' do
let(:attributes) { {} }
let(:work_package) { new_work_package }
- before do
- work_package.priority = nil
+ context 'with the parent having dates and not providing own dates' do
+ let(:call_attributes) { { parent: parent } }
+
+ it_behaves_like 'service call' do
+ it "sets the start_date to the parent`s start_date" do
+ subject
+
+ expect(work_package.start_date)
+ .to eql parent_start_date
+ end
+
+ it "sets the due_date to the parent`s due_date" do
+ subject
+
+ expect(work_package.due_date)
+ .to eql parent_due_date
+ end
+ end
end
- it_behaves_like 'service call' do
- it "sets the default priority" do
- subject
+ context 'with the parent having dates but providing own dates' do
+ let(:call_attributes) { { parent: parent, start_date: Date.today, due_date: Date.today + 1.day } }
- expect(work_package.priority)
- .to eql default_priority
+ it_behaves_like 'service call' do
+ it "sets the start_date to the provided date" do
+ subject
+
+ expect(work_package.start_date)
+ .to eql Date.today
+ end
+
+ it "sets the due_date to the provided date" do
+ subject
+
+ expect(work_package.due_date)
+ .to eql Date.today + 1.day
+ end
+ end
+ end
+
+ context 'with the parent having dates but providing own start_date' do
+ let(:call_attributes) { { parent: parent, start_date: Date.today } }
+
+ it_behaves_like 'service call' do
+ it "sets the start_date to the provided date" do
+ subject
+
+ expect(work_package.start_date)
+ .to eql Date.today
+ end
+
+ it "sets the due_date to the parent's due_date" do
+ subject
+
+ expect(work_package.due_date)
+ .to eql parent_due_date
+ end
+ end
+ end
+
+ context 'with the parent having dates but providing own due_date' do
+ let(:call_attributes) { { parent: parent, due_date: Date.today + 4.days } }
+
+ it_behaves_like 'service call' do
+ it "sets the start_date to the parent's start date" do
+ subject
+
+ expect(work_package.start_date)
+ .to eql parent_start_date
+ end
+
+ it "sets the due_date to the provided date" do
+ subject
+
+ expect(work_package.due_date)
+ .to eql Date.today + 4.days
+ end
+ end
+ end
+
+ context 'with the parent having dates but providing own empty start_date' do
+ let(:call_attributes) { { parent: parent, start_date: nil } }
+
+ it_behaves_like 'service call' do
+ it "sets the start_date to nil" do
+ subject
+
+ expect(work_package.start_date)
+ .to be_nil
+ end
+
+ it "sets the due_date to the parent's due_date" do
+ subject
+
+ expect(work_package.due_date)
+ .to eql parent_due_date
+ end
+ end
+ end
+
+ context 'with the parent having dates but providing own empty due_date' do
+ let(:call_attributes) { { parent: parent, due_date: nil } }
+
+ it_behaves_like 'service call' do
+ it "sets the start_date to the parent's start date" do
+ subject
+
+ expect(work_package.start_date)
+ .to eql parent_start_date
+ end
+
+ it "sets the due_date to nil" do
+ subject
+
+ expect(work_package.due_date)
+ .to be_nil
+ end
+ end
+ end
+
+ context 'with the parent having dates but providing a start date that is before parent`s due date`' do
+ let(:call_attributes) { { parent: parent, start_date: parent_due_date - 4.days } }
+
+ it_behaves_like 'service call' do
+ it "sets the start_date to the provided date" do
+ subject
+
+ expect(work_package.start_date)
+ .to eql parent_due_date - 4.days
+ end
+
+ it "sets the due_date to the parent's due_date" do
+ subject
+
+ expect(work_package.due_date)
+ .to eql parent_due_date
+ end
+ end
+ end
+
+ context 'with the parent having dates but providing a start date that is after the parent`s due date`' do
+ let(:call_attributes) { { parent: parent, start_date: parent_due_date + 1.day } }
+
+ it_behaves_like 'service call' do
+ it "sets the start_date to the provided date" do
+ subject
+
+ expect(work_package.start_date)
+ .to eql parent_due_date + 1.day
+ end
+
+ it "leaves the due date empty" do
+ subject
+
+ expect(work_package.due_date)
+ .to be_nil
+ end
+ end
+ end
+
+ context 'with the parent having dates but providing a due date that is before the parent`s start date`' do
+ let(:call_attributes) { { parent: parent, due_date: parent_start_date - 3.day } }
+
+ it_behaves_like 'service call' do
+ it "leaves the start date empty" do
+ subject
+
+ expect(work_package.start_date)
+ .to be_nil
+ end
+
+ it "set the due date to the provided date" do
+ subject
+
+ expect(work_package.due_date)
+ .to eql parent_start_date - 3.day
+ end
end
end
end
- context 'start_date with default setting', with_settings: { work_package_startdate_is_adddate: true } do
+ context 'with default setting', with_settings: { work_package_startdate_is_adddate: true } do
context 'no value set before for a new work package' do
let(:call_attributes) { {} }
let(:attributes) { {} }
let(:work_package) { new_work_package }
- before do
- work_package.priority = nil
- end
-
it_behaves_like 'service call' do
it "sets the default priority" do
subject
- expect(work_package.priority)
- .to eql default_priority
+ expect(work_package.start_date)
+ .to eql Date.today
end
end
end
@@ -358,6 +519,36 @@ describe WorkPackages::SetAttributesService, type: :model do
end
end
end
+ end
+
+ context 'priority' do
+ let(:default_priority) { FactoryBot.build_stubbed(:priority) }
+ let(:other_priority) { FactoryBot.build_stubbed(:priority) }
+
+ before do
+ allow(IssuePriority)
+ .to receive_message_chain(:active, :default)
+ .and_return(default_priority)
+ end
+
+ context 'no value set before for a new work package' do
+ let(:call_attributes) { {} }
+ let(:attributes) { {} }
+ let(:work_package) { new_work_package }
+
+ before do
+ work_package.priority = nil
+ end
+
+ it_behaves_like 'service call' do
+ it "sets the default priority" do
+ subject
+
+ expect(work_package.priority)
+ .to eql default_priority
+ end
+ end
+ end
context 'update priority before calling the service' do
let(:call_attributes) { {} }
diff --git a/spec/support/edit_fields/date_edit_field.rb b/spec/support/edit_fields/date_edit_field.rb
index a38bb57ebd2..bc7c9e804dc 100644
--- a/spec/support/edit_fields/date_edit_field.rb
+++ b/spec/support/edit_fields/date_edit_field.rb
@@ -66,7 +66,13 @@ class DateEditField < EditField
end
def input_element
- modal_element.find(input_selector)
+ # The date picker might not be opened but the input might still be visible,
+ # e.g. when the work package form is opened completely like on create
+ if active?
+ modal_element.find(input_selector)
+ else
+ page.find(".#{property_name} input")
+ end
end
def active?
@@ -109,8 +115,7 @@ class DateEditField < EditField
end
def expect_value(value)
- expect
- expect(input_element.text).to eq(value)
+ expect(input_element.value).to eq(value)
end
def select_value(value)
diff --git a/spec/support/pages/work_packages/abstract_work_package.rb b/spec/support/pages/work_packages/abstract_work_package.rb
index 1bcd6eadc18..d060422a047 100644
--- a/spec/support/pages/work_packages/abstract_work_package.rb
+++ b/spec/support/pages/work_packages/abstract_work_package.rb
@@ -193,7 +193,7 @@ module Pages
when /customField(\d+)$/
work_package_custom_field(key, $1)
when :date, :startDate, :dueDate, :combinedDate
- DateEditField.new container, key, is_milestone: work_package.milestone?
+ DateEditField.new container, key, is_milestone: work_package&.milestone?
when :description
TextEditorField.new container, key
when :status