Passed
Push — master ( 0c064b...50f875 )
by Christian
15:00 queued 10s
created

  A

Complexity

Conditions 5

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

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