|
| 1 | +import { revalidateTag } from "next/cache"; |
| 2 | + |
1 | 3 | export const UserService = (() => {
|
2 | 4 | const usersEndpoint = process.env.CANNON_URL + "/users";
|
| 5 | + const filesEndpoint = process.env.CANNON_URL + "/files"; |
3 | 6 |
|
4 | 7 | const getMe = async (cannonToken: string) => {
|
5 |
| - const resp = await fetch(usersEndpoint + "/me", { |
6 |
| - headers: { |
7 |
| - "Content-Type": "application/json", |
8 |
| - Authorization: `Bearer ${cannonToken}`, |
9 |
| - }, |
10 |
| - next: { |
11 |
| - revalidate: 300, // 5 mins |
12 |
| - tags: ["modified-me"], |
13 |
| - }, |
14 |
| - }); |
15 |
| - |
16 |
| - if (resp.ok) return resp.json(); |
| 8 | + try { |
| 9 | + const resp = await fetch(usersEndpoint + "/me", { |
| 10 | + headers: { |
| 11 | + "Content-Type": "application/json", |
| 12 | + Authorization: `Bearer ${cannonToken}`, |
| 13 | + }, |
| 14 | + next: { |
| 15 | + revalidate: 300, // 5 mins |
| 16 | + tags: ["modified-me"], |
| 17 | + }, |
| 18 | + }); |
| 19 | + |
| 20 | + if (resp.ok) return resp.json(); |
| 21 | + |
| 22 | + } catch (error) { |
| 23 | + console.error(error); |
| 24 | + } |
17 | 25 | return null;
|
18 | 26 | };
|
19 | 27 |
|
20 | 28 | const demoteMe = async (cannonToken: string) => {
|
21 |
| - const resp = await fetch(usersEndpoint + "/me", { |
22 |
| - method: "PUT", |
23 |
| - headers: { |
24 |
| - "Content-Type": "application/json", |
25 |
| - Authorization: `Bearer ${cannonToken}`, |
26 |
| - }, |
27 |
| - body: JSON.stringify({ role: "user" }), |
28 |
| - }); |
29 |
| - |
30 |
| - if (resp.ok) return true; |
31 |
| - return false; |
| 29 | + let success = false; |
| 30 | + |
| 31 | + try { |
| 32 | + const resp = await fetch(usersEndpoint + "/me", { |
| 33 | + method: "PUT", |
| 34 | + headers: { |
| 35 | + "Content-Type": "application/json", |
| 36 | + Authorization: `Bearer ${cannonToken}`, |
| 37 | + }, |
| 38 | + body: JSON.stringify({ role: "user" }), |
| 39 | + }); |
| 40 | + |
| 41 | + if (resp.ok) success = true; |
| 42 | + |
| 43 | + } catch (error) { |
| 44 | + console.error(error); |
| 45 | + } |
| 46 | + return success; |
| 47 | + }; |
| 48 | + |
| 49 | + const getCVInfo = async (cannonToken: string) => { |
| 50 | + try { |
| 51 | + const resp = await fetch(filesEndpoint + "/me", { |
| 52 | + method: "GET", |
| 53 | + headers: { |
| 54 | + Authorization: `Bearer ${cannonToken}`, |
| 55 | + }, |
| 56 | + next: { |
| 57 | + revalidate: 86400, // 1 day |
| 58 | + tags: ["modified-cv"], |
| 59 | + }, |
| 60 | + }); |
| 61 | + if (resp.ok) return resp.json(); |
| 62 | + |
| 63 | + } catch (error) { |
| 64 | + console.log(error); |
| 65 | + } |
| 66 | + return null; |
| 67 | + }; |
| 68 | + |
| 69 | + const uploadCV = async (cannonToken: string, cv: File) => { |
| 70 | + let success = false; |
| 71 | + |
| 72 | + try { |
| 73 | + let formData = new FormData(); |
| 74 | + formData.append("file", cv, cv.name); |
| 75 | + |
| 76 | + const resp = await fetch(filesEndpoint + "/me", { |
| 77 | + method: "POST", |
| 78 | + headers: { |
| 79 | + Authorization: `Bearer ${cannonToken}`, |
| 80 | + }, |
| 81 | + body: formData, |
| 82 | + }); |
| 83 | + |
| 84 | + if (resp.ok) { |
| 85 | + revalidateTag("modified-cv"); |
| 86 | + success = true; |
| 87 | + } |
| 88 | + |
| 89 | + } catch (error) { |
| 90 | + console.error(error); |
| 91 | + } |
| 92 | + return success; |
| 93 | + }; |
| 94 | + |
| 95 | + const deleteCV = async (cannonToken: string) => { |
| 96 | + let success = false; |
| 97 | + |
| 98 | + try { |
| 99 | + const resp = await fetch(filesEndpoint + "/me", { |
| 100 | + method: "DELETE", |
| 101 | + headers: { |
| 102 | + Authorization: `Bearer ${cannonToken}`, |
| 103 | + }, |
| 104 | + }); |
| 105 | + |
| 106 | + if (resp.ok) { |
| 107 | + revalidateTag("modified-cv"); |
| 108 | + success = true; |
| 109 | + } |
| 110 | + |
| 111 | + } catch (error) { |
| 112 | + console.log(error); |
| 113 | + } |
| 114 | + return success; |
32 | 115 | };
|
33 | 116 |
|
34 |
| - return { getMe, demoteMe }; |
| 117 | + return { getMe, demoteMe, getCVInfo, uploadCV, deleteCV }; |
35 | 118 | })();
|
0 commit comments