-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.ts
141 lines (124 loc) · 3.56 KB
/
utils.ts
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
import jwt, { Secret } from 'jsonwebtoken';
import cloudinary from 'cloudinary';
import fetch from 'node-fetch';
import { Context, ProcessUpload } from './api/context';
require('custom-env').env('dev');
const {
CLOUDINARY_CLOUD_NAME,
CLOUDINARY_API_KEY,
CLOUDINARY_API_SECRET,
CLOUDINARY_UPLOAD_PRESET,
APP_SECRET_CODE,
MESSAGEBIRD_BASE_URL,
MESSAGEBIRD_API_KEY
} = process.env;
cloudinary.v2.config({
cloud_name: CLOUDINARY_CLOUD_NAME,
api_key: CLOUDINARY_API_KEY,
api_secret: CLOUDINARY_API_SECRET
});
const getUserId = (context: Context): string => {
const Authorization = context.request.get('Authorization');
if (Authorization) {
const token = Authorization.replace('Bearer ', '');
const { userId } = jwt.verify(token, APP_SECRET_CODE as Secret) as {
userId: string;
};
return userId;
}
throw new Error('Not authenticated');
};
const processUpload: ProcessUpload = async upload => {
const dataInBase64 = await upload.uri;
let resultUrl = '';
const cloudinaryUpload = async (data: string): Promise<void> => {
try {
const results = await cloudinary.v2.uploader.upload(data, {
upload_preset: CLOUDINARY_UPLOAD_PRESET,
transformation: { width: 300 }
});
resultUrl = await results.url;
} catch (err) {
throw new Error(`Failed to upload profile picture ! Err:${err.message}`);
}
};
await cloudinaryUpload(dataInBase64);
return resultUrl;
};
const authentication_step_one = async (
numero: string
): Promise<{ id: string; status: string }> => {
// make 00337 to 337
const slicedNumero = numero.slice(2);
const opts = {
//TODO change this number
recipient: slicedNumero,
originator: 'Heelkr App',
timeout: 60,
template:
'Votre code de vérification est %token. Veuillez ne pas le communiquer à une tierce entité.'
};
const response = await fetch(`${MESSAGEBIRD_BASE_URL}verify`, {
method: 'post',
headers: {
'Content-Type': 'application/json',
Authorization: `AccessKey test_${MESSAGEBIRD_API_KEY}`
},
body: JSON.stringify(opts)
});
const { id, status } = await response.json();
return { id, status };
};
const authentication_step_two = async (
id: string,
token: string
): Promise<{ success: boolean }> => {
const response = await fetch(
`${MESSAGEBIRD_BASE_URL}verify/${id}/?token=${token}`,
{
method: 'get',
headers: {
'Content-Type': 'application/json',
Authorization: `AccessKey test_${MESSAGEBIRD_API_KEY}`
}
}
);
const { status = '', errors = '' } = await response.json();
if (errors || status != 'verified') return { success: false };
return { success: true };
};
const addFunction = (a: number, b: number): number => a + b;
// Can use this function below, OR use Expo's Push Notification Tool-> https://expo.io/dashboard/notifications
const sendPushNotification = async (
expoPushToken: string,
payload: stringOrObject[]
): Promise<void> => {
if (!expoPushToken) return;
const [title, body, data] = payload;
const message = {
to: expoPushToken,
sound: 'default',
title,
body,
data
};
await fetch('https://exp.host/--/api/v2/push/send', {
method: 'POST',
headers: {
Accept: 'application/json',
'Accept-encoding': 'gzip, deflate',
'Content-Type': 'application/json'
},
body: JSON.stringify(message)
});
};
export type stringOrObject = string | object;
export {
APP_SECRET_CODE,
getUserId,
processUpload,
authentication_step_one,
authentication_step_two,
addFunction,
sendPushNotification
};