{"size": 2593, "ext": "js", "max_stars_repo_path": "src/Administration/Resources/app/administration/src/module/sw-bulk-edit/service/handler/bulk-edit-order.handler.js", "max_stars_repo_name": "RakeshJesadiya/platform", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/Administration/Resources/app/administration/src/module/sw-bulk-edit/service/handler/bulk-edit-order.handler.js", "max_issues_repo_name": "RakeshJesadiya/platform", "max_issues_repo_issues_event_min_datetime": "2021-08-29T06:51:20.000Z", "max_issues_repo_issues_event_max_datetime": "2022-02-27T22:33:42.000Z", "max_forks_repo_path": "src/Administration/Resources/app/administration/src/module/sw-bulk-edit/service/handler/bulk-edit-order.handler.js", "max_forks_repo_name": "RakeshJesadiya/platform", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 6, "max_forks_count": null, "avg_line_length": 32.8227848101, "max_line_length": 119, "alphanum_fraction": 0.4708831469}
import BulkEditBaseHandler from './bulk-edit-base.handler';

const { Criteria } = Shopware.Data;

/**
 * @class
 * @extends BulkEditBaseHandler
 */
class BulkEditOrderHandler extends BulkEditBaseHandler {
    constructor() {
        super();
        this.name = 'BulkEditOrderHandler';
        this.entityIds = [];
        this.orderStateMachineService = Shopware.Service('orderStateMachineService');
        this.orderRepository = Shopware.Service('repositoryFactory').create('order');
    }

    async bulkEdit(entityIds, payload) {
        this.entityIds = entityIds;

        let promises = [];

        try {
            const orders = await this.orderRepository.search(this.getCriteria());

            payload.forEach((change) => {
                if (!change.value) {
                    return;
                }

                promises = orders.map((order) => {
                    const optionsMail = {
                        documentIds: [
                            // TODO: NEXT-15616 - allow sending email for status changes including document attachments
                        ],
                        sendMail: change.sendMail,
                    };

                    switch (change.field) {
                        case 'orderTransactions':
                            return this.orderStateMachineService.transitionOrderTransactionState(
                                order.transactions.first()?.id,
                                change.value,
                                optionsMail,
                            );
                        case 'orderDeliveries':
                            return this.orderStateMachineService.transitionOrderDeliveryState(
                                order.deliveries.first()?.id,
                                change.value,
                                optionsMail,
                            );
                        default:
                            return this.orderStateMachineService.transitionOrderState(
                                order.id,
                                change.value,
                                optionsMail,
                            );
                    }
                });
            });
        } catch (e) {
            throw e;
        }

        return Promise.all(promises);
    }

    getCriteria() {
        const criteria = new Criteria();
        criteria.setIds(this.entityIds);
        criteria.getAssociation('deliveries');
        criteria.getAssociation('transactions');

        return criteria;
    }
}

export default BulkEditOrderHandler;
