This repository has been archived by the owner on Sep 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0c5433e
commit 862c087
Showing
8 changed files
with
171 additions
and
59 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
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,5 @@ | ||
export const enum ACTION { | ||
DELETE = "delete", | ||
UPDATE = "update", | ||
CREATE = "create", | ||
} |
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
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
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,28 +1,110 @@ | ||
/* eslint-disable camelcase */ | ||
import { createClient } from "@supabase/supabase-js"; | ||
import { AnyThreadChannel } from "discord.js"; | ||
|
||
import { Database } from "../database.types"; | ||
import { ACTION } from "../events/action.types"; | ||
import { ExtendedClient } from "../interfaces/ExtendedClient"; | ||
import { threadTransform } from "./transformers"; | ||
|
||
// const supabase = createClient<Database>( | ||
// process.env.SUPABASE_URL || "", | ||
// process.env.SUPABASE_ANON_KEY || "" | ||
// ); | ||
import { threadAction } from "./transformers"; | ||
|
||
const timeZone = "America/New_York"; | ||
|
||
const supabase = createClient<Database>( | ||
process.env.SUPABASE_URL || "", | ||
process.env.SUPABASE_ANON_KEY || "" | ||
); | ||
|
||
/** | ||
* | ||
* @param action | ||
* @param bot | ||
* @param data | ||
* @param {typeof ACTION} action - Create, update or delete. | ||
* @param {ExtendedClient} bot - Bot instance. | ||
* @param {AnyThreadChannel} thread - Thread instance. | ||
* @returns {any} - Any. | ||
*/ | ||
export const sendToSupabase = async ( | ||
action: string, | ||
bot: ExtendedClient, | ||
data: AnyThreadChannel | ||
thread: AnyThreadChannel | ||
) => { | ||
// if (supabase) { | ||
switch (action) { | ||
case ACTION.CREATE: { | ||
const threadData = await threadAction().create(thread); | ||
const threadJson = { | ||
raw: JSON.parse(JSON.stringify(threadData)), | ||
source: "discord", | ||
origin: threadData.id, | ||
created_at: | ||
thread.createdAt?.toLocaleString("en-US", { timeZone }) || | ||
new Date().toLocaleString("en-US", { timeZone }), | ||
}; | ||
const { data, error } = await supabase | ||
.from("community_raw") | ||
.insert([threadJson]) | ||
.select(); | ||
if (error) { | ||
return Promise.reject(error); | ||
} | ||
return data; | ||
} | ||
case ACTION.UPDATE: { | ||
const threadData = await threadAction().update(thread); | ||
const threadJson = { | ||
raw: JSON.parse(JSON.stringify(threadData)), | ||
source: "discord", | ||
origin: threadData.id, | ||
created_at: | ||
thread.createdAt?.toLocaleString("en-US", { timeZone }) || | ||
new Date().toLocaleString("en-US", { timeZone }), | ||
}; | ||
|
||
const { data, error } = await supabase | ||
.from("community_raw") | ||
.update({ ...threadJson }) | ||
.eq("source", threadData.id) | ||
.select(); | ||
|
||
if (error) { | ||
return Promise.reject(error); | ||
} | ||
|
||
return data; | ||
} | ||
case ACTION.DELETE: { | ||
const { data, error } = await supabase | ||
.from("community_raw") | ||
.select("*") | ||
.eq("source", thread.id); | ||
|
||
if (data && data.length > 0) { | ||
const { raw, ...rest } = data[0]; | ||
const deletedRawData = Object.assign( | ||
{}, | ||
JSON.parse(JSON.stringify(raw)), | ||
// How to convert the JSON to object, what is this typescript error | ||
{ | ||
deleted: true, | ||
} | ||
); | ||
|
||
const { data: deletedPost, error: deletePostError } = await supabase | ||
.from("community_raw") | ||
.update({ ...rest, ...deletedRawData }) | ||
.eq("source", thread.id) | ||
.select(); | ||
|
||
// } | ||
await threadTransform(data); | ||
if (deletePostError) { | ||
return Promise.reject(deletePostError); | ||
} | ||
return deletedPost; | ||
} | ||
if (error) { | ||
return Promise.reject(error); | ||
} | ||
return; | ||
} | ||
default: { | ||
return; | ||
} | ||
} | ||
}; |
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