Passed
Push — master ( 39957b...28ab06 )
by Christian
13:18 queued 11s
created

  B

Complexity

Conditions 6

Size

Total Lines 30
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 17
c 0
b 0
f 0
dl 0
loc 30
rs 8.6166
1
export default class ShopwareDiscountCampaignService {
2
    isDiscountCampaignActive(discountCampaign) {
3
        if (!discountCampaign) {
4
            return false;
5
        }
6
7
        if (!discountCampaign.startDate) {
8
            return false;
9
        }
10
11
        const now = new Date();
12
13
        if (new Date(discountCampaign.startDate) > now) {
14
            return false;
15
        }
16
17
        if (typeof discountCampaign.endDate === 'string' &&
18
            new Date(discountCampaign.endDate) < now
19
        ) {
20
            return false;
21
        }
22
23
        if (typeof discountCampaign.discountAppliesForMonths === 'number' &&
24
            discountCampaign.discountAppliesForMonths === 0
25
        ) {
26
            return false;
27
        }
28
29
        // discounts without end date are always valid
30
        return true;
31
    }
32
33
    isSamePeriod(discountCampaign, comparator) {
34
        const discountDuration = discountCampaign.discountAppliesForMonths || null;
35
        const comparatorDuration = comparator.discountAppliesForMonths || null;
36
37
        return discountCampaign.startDate === comparator.startDate &&
38
            discountCampaign.endDate === comparator.endDate &&
39
            discountDuration === comparatorDuration;
40
    }
41
}
42