File size: 6,256 Bytes
2fcec7b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
angular.module('carli.cycleService')
    .service('cycleService', cycleService);

function cycleService( CarliModules, $q, errorHandler, persistentState ) {

    var cycleModule = CarliModules.Cycle;
    var cycleMiddleware = CarliModules.CycleMiddleware;
    var databaseStatusMiddleware = CarliModules.DatabaseStatusMiddleware;
    var VendorDatabaseModule = CarliModules.VendorDatabaseMiddleware;
    var libraryMiddlewareModule = CarliModules.LibraryMiddleware;

    var currentCycle = null;

    var fakeProgress = {
        replication: 0,
        viewIndexing: 0,
        offeringTransformation: 0
    };

    return {
        cycleDefaults: cycleDefaults,
        list: function() { return $q.when( cycleModule.list()).catch(errorHandler); },
        listActiveCycles: listActiveCycles,
        listActiveCyclesOfType: listActiveCyclesOfType,
        listActiveSubscriptionCycles: listActiveSubscriptionCycles,
        listNonArchivedClosedCyclesIncludingOneTimePurchase: listNonArchivedClosedCyclesIncludingOneTimePurchase,
        listSelectionsForCycle: listSelectionsForCycle,
        create: function(newCycle) {
            fixCycleName(newCycle);
            return $q.when(cycleMiddleware.create(newCycle));
        },
        createCycleFrom: function( sourceCycle, newCycle ) {
            fixCycleName(newCycle);
            return $q.when(cycleMiddleware.createCycleFrom(sourceCycle,newCycle));
        },
        getCycleCreationStatus: function(cycleId){
            return $q.when( cycleMiddleware.getCycleCreationStatus(cycleId) )
                .then(function (statusString) {
                    return statusString;
                });
        },
        getCycleDatabaseStatuses: getCycleDatabaseStatuses,
        update: function() { return $q.when( cycleModule.update.apply( this, arguments) ); },
        load:   function() { return $q.when( cycleModule.load.apply( this, arguments) ).catch(errorHandler); },
        getCurrentCycle: getCurrentCycle,
        setCurrentCycle: setCurrentCycle,
        initCurrentCycle: function(){
            listActiveCycles().then(function(cycleList){
                currentCycle = cycleList[0];
            });
        },
        syncDataToVendorDatabase: syncDataToVendorDatabase,
        syncDataToAllVendorDatabases: syncDataToAllVendorDatabases,
        syncDataToAllVendorDatabasesForCycle: syncDataToAllVendorDatabasesForCycle,
        listPastFourCyclesMatchingCycle: function( cycle ){
            return $q.when( cycleModule.listPastFourCyclesMatchingCycle(cycle || currentCycle) );
        },
        deleteCycle: deleteCycle,
        generateCycleName: generateCycleName,
        getLabelForCycleStatus: cycleModule.getStatusLabel,
        fiscalYearHasStartedForDate: cycleModule.fiscalYearHasStartedForDate,
        CYCLE_STATUS_DATA_PROCESSING: cycleModule.CYCLE_STATUS_DATA_PROCESSING,
        CYCLE_STATUS_EDITING_PRODUCT_LIST: cycleModule.CYCLE_STATUS_EDITING_PRODUCT_LIST,
        CYCLE_STATUS_VENDOR_PRICING: cycleModule.CYCLE_STATUS_VENDOR_PRICING,
        CYCLE_STATUS_CHECKING_PRICES: cycleModule.CYCLE_STATUS_CHECKING_PRICES,
        CYCLE_STATUS_OPEN_TO_LIBRARIES: cycleModule.CYCLE_STATUS_OPEN_TO_LIBRARIES,
        CYCLE_STATUS_CLOSED: cycleModule.CYCLE_STATUS_CLOSED,
        CYCLE_STATUS_ARCHIVED: cycleModule.CYCLE_STATUS_ARCHIVED
    };

    function fixCycleName(newCycle) {
        newCycle.name = generateCycleName(newCycle);
        delete newCycle.description;
    }

    function cycleDefaults() {
        return  {
            status: cycleModule.CYCLE_STATUS_DATA_PROCESSING,
            isArchived: false
        };
    }
    function generateCycleName(cycle) {
        return (cycle.cycleType == 'Alternative Cycle') ?
            cycle.description + ' ' + cycle.year :
            cycle.cycleType + ' ' + cycle.year;
    }

    function getCycleDatabaseStatuses(cycleId) {
        return $q.when( databaseStatusMiddleware.getCycleStatusForAllVendors(cycleId) );
    }

    function listActiveCycles() {
        return $q.when( cycleModule.listActiveCycles() ).catch(errorHandler);
    }

    function listActiveSubscriptionCycles() {
        return listActiveCyclesExcludingType('One-Time Purchase');
    }

    function listActiveCyclesExcludingType(type){
        function filterMatchingType(cycle) {
            return (cycle.cycleType !== type);
        }

        return listActiveCycles().then(function(activeCycleList){
            return activeCycleList.filter(filterMatchingType);
        });
    }

    function listActiveCyclesOfType(type){
        function filterMatchingType(cycle) {
            return (cycle.cycleType == type);
        }

        return listActiveCycles().then(function(activeCycleList){
            return activeCycleList.filter(filterMatchingType);
        });
    }

    function listNonArchivedClosedCyclesIncludingOneTimePurchase(){
        return listActiveCycles()
            .then(function( cycleList ){
                return cycleList.filter(cycleIsClosed);
            })
            .catch(errorHandler);

        function cycleIsClosed( cycle ){
            return cycle.status === cycleModule.CYCLE_STATUS_CLOSED;
        }
    }

    function listSelectionsForCycle( cycle, libraryId ){
        return $q.when(libraryMiddlewareModule.listSelectionsForLibraryFromCycle(libraryId, cycle.id))
            .catch(errorHandler);
    }

    function getCurrentCycle() {
        return currentCycle;
    }

    function setCurrentCycle(cycleObject) {
        currentCycle = cycleObject;
        persistentState.setCurrentCycle(cycleObject);
    }

    function syncDataToVendorDatabase(vendorId, cycle){
        var cycleId = cycle ? cycle.id : currentCycle.id;
        return $q.when( VendorDatabaseModule.replicateDataToOneVendorForCycle(vendorId, cycleId) );
    }

    function syncDataToAllVendorDatabases(){
        return syncDataToAllVendorDatabasesForCycle();
    }

    function syncDataToAllVendorDatabasesForCycle(cycle){
        var cycleId = cycle ? cycle.id : currentCycle.id;
        return $q.when( VendorDatabaseModule.replicateDataToVendorsForCycle(cycleId) );
    }

    function deleteCycle(cycleObject) {
        return $q.when(cycleMiddleware.deleteCycle(cycleObject.id));
    }
}