-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.js
More file actions
104 lines (98 loc) · 3.3 KB
/
Copy pathutils.js
File metadata and controls
104 lines (98 loc) · 3.3 KB
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
const { Transform, } = require('stream');
const fs = require('fs');
const jwt = require('jsonwebtoken');
const { format, parse, } = require('date-fns');
const { iss, kid, } = require('./keys');
exports.tokenGen = () => {
const cert = fs.readFileSync('AuthKey.p8');
const payload = {
iss,
exp: Math.floor(Date.now() / 1000) + (20 * 60),
aud: "appstoreconnect-v1"
};
const header = {
alg: "ES256",
kid,
typ: "JWT"
};
console.log('generating app store connect jwt');
return jwt.sign(payload, cert, { header, });
};
exports.tsvToCsv = () => new Transform({
transform(chunk, encoding, done) {
this.push(chunk.toString().replace(/\t/g, ','));
done();
}
});
exports.addUsdPrice = (type, rates, formattedDate) => new Transform({
transform(chunk, encoding, done) {
const obj = JSON.parse(chunk.toString());
switch (type) {
case 'SUBSCRIBER': {
const {
'Customer Price': customer_price,
'Customer Currency': customer_currency,
'Developer Proceeds': developer_proceeds,
'Proceeds Currency': currency_of_proceeds,
'Event Date': event_date,
} = obj;
const day = rates.find(({ date, }) => date === format(parse(event_date), 'YYYY-MM-DD'));
const result = {
...obj,
customer_price_in_usd: Math.round(customer_price / day[customer_currency] * 100) / 100,
developer_proceeds_in_usd: currency_of_proceeds
? Math.round(developer_proceeds / day[currency_of_proceeds] * 100) / 100
: 0,
};
this.push(JSON.stringify(result));
break;
}
case 'SALES': {
const {
'Customer Price': customer_price,
'Customer Currency': customer_currency,
'Developer Proceeds': developer_proceeds,
'Currency of Proceeds': currency_of_proceeds,
'Begin Date': begin_date,
} = obj;
const day = rates.find(({ date, }) => date === format(parse(begin_date), 'YYYY-MM-DD'));
const result = {
...obj,
customer_price_in_usd: Math.round(customer_price / day[customer_currency] * 100) / 100,
developer_proceeds_in_usd: currency_of_proceeds
? Math.round(developer_proceeds / day[currency_of_proceeds] * 100) / 100
: 0,
};
this.push(JSON.stringify(result));
break;
}
case 'SUBSCRIPTION': {
const {
'Customer Price': customer_price,
'Customer Currency': customer_currency,
'Developer Proceeds': developer_proceeds,
'Proceeds Currency': currency_of_proceeds,
} = obj;
const day = rates.find(({ date, }) => date === formattedDate);
const result = {
...obj,
event_date: formattedDate,
customer_price_in_usd: Math.round(customer_price / day[customer_currency] * 100) / 100,
developer_proceeds_in_usd: currency_of_proceeds
? Math.round(developer_proceeds / day[currency_of_proceeds] * 100) / 100
: 0,
};
this.push(JSON.stringify(result));
break;
}
default: {
this.push(chunk);
}
}
done();
}
});
exports.fileWriteStreamPromise = file => new Promise((resolve, reject) => {
file.on('finish', resolve);
file.on('error', reject);
});