-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from Real-Dev-Squad/Feature/1119-OOO-Nickname-…
…Update Feat: OOO Nickname Update Job
- Loading branch information
Showing
5 changed files
with
102 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
const NAMESPACE_NAME = 'CronJobsTimestamp'; | ||
|
||
export { NAMESPACE_NAME }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,66 @@ | ||
import { KVNamespace } from '@cloudflare/workers-types'; | ||
|
||
import { handleConfig } from '../config/config'; | ||
import { env } from '../types/global.types'; | ||
import { NAMESPACE_NAME } from '../constants'; | ||
import { env, NicknameUpdateResponseType } from '../types/global.types'; | ||
import { generateJwt } from '../utils/generateJwt'; | ||
|
||
export async function ping(env: env) { | ||
const url = handleConfig(env); | ||
const response = await fetch(`${url.baseUrl}/healthcheck`); | ||
return response; | ||
} | ||
|
||
export async function callDiscordNicknameBatchUpdate(env: env) { | ||
const namespace = env[NAMESPACE_NAME] as unknown as KVNamespace; | ||
let lastNicknameUpdate: string | null = '0'; | ||
try { | ||
lastNicknameUpdate = await namespace.get('DISCORD_NICKNAME_UPDATED_TIME'); | ||
if (lastNicknameUpdate === null) { | ||
throw new Error('Error while fetching KV "DISCORD_NICKNAME_UPDATED_TIME" timestamp'); | ||
} | ||
if (!lastNicknameUpdate) { | ||
lastNicknameUpdate = '0'; | ||
} | ||
} catch (err) { | ||
console.error(err, 'Error while fetching the timestamp for last nickname update'); | ||
throw err; | ||
} | ||
|
||
const url = handleConfig(env); | ||
let token; | ||
try { | ||
token = await generateJwt(env); | ||
} catch (err) { | ||
console.error(`Error while generating JWT token: ${err}`); | ||
throw err; | ||
} | ||
const response = await fetch(`${url.baseUrl}/discord-actions/nickname/status`, { | ||
method: 'POST', | ||
headers: { | ||
Authorization: `Bearer ${token}`, | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify({ | ||
lastNicknameUpdate, | ||
}), | ||
}); | ||
if (!response.ok) { | ||
throw new Error("Error while trying to update users' discord nickname"); | ||
} | ||
|
||
const data: NicknameUpdateResponseType = await response.json(); | ||
if (data?.data.totalUsersStatus !== 0 && data?.data.successfulNicknameUpdates === 0) { | ||
throw new Error("Error while trying to update users' discord nickname"); | ||
} | ||
|
||
console.log(data); | ||
|
||
try { | ||
await namespace.put('DISCORD_NICKNAME_UPDATED_TIME', Date.now().toString()); | ||
} catch (err) { | ||
console.error('Error while trying to update the last nickname change timestamp'); | ||
} | ||
|
||
return data; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,12 @@ | ||
export type env = { | ||
[key: string]: string; | ||
}; | ||
|
||
export type NicknameUpdateResponseType = { | ||
message: string; | ||
data: { | ||
totalUsersStatus: number; | ||
successfulNicknameUpdates: number; | ||
unsuccessfulNicknameUpdates: number; | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters