feat: move appwrite to entgamers-database package
This commit is contained in:
+2
-6
@@ -21,10 +21,6 @@ NEXT_PUBLIC_APPWRITE_ENDPOINT=""
|
|||||||
NEXT_PUBLIC_APPWRITE_PROJECT_ID=""
|
NEXT_PUBLIC_APPWRITE_PROJECT_ID=""
|
||||||
APPWRITE_API_KEY=""
|
APPWRITE_API_KEY=""
|
||||||
|
|
||||||
# Appwrite optional variables
|
# Prisma required variables
|
||||||
|
|
||||||
NEXT_PUBLIC_APPWRITE_DATABASE_ID=""
|
DATABASE_URL=""
|
||||||
NEXT_PUBLIC_APPWRITE_COLLECTION_ID=""
|
|
||||||
NEXT_PUBLIC_APPWRITE_ADMIN_TEAM_ID=""
|
|
||||||
NEXT_PUBLIC_APPWRITE_ADMIN_MODERATOR_TEAM_ID=""
|
|
||||||
NEXT_PUBLIC_APPWRITE_ADMIN_COLLABORATOR_TEAM_ID=""
|
|
||||||
@@ -18,6 +18,7 @@
|
|||||||
"@fortawesome/free-solid-svg-icons": "^6.5.1",
|
"@fortawesome/free-solid-svg-icons": "^6.5.1",
|
||||||
"@fortawesome/react-fontawesome": "^0.2.0",
|
"@fortawesome/react-fontawesome": "^0.2.0",
|
||||||
"appwrite": "^13.0.1",
|
"appwrite": "^13.0.1",
|
||||||
|
"entgamers-database": "^0.0.5",
|
||||||
"entgamers-panda-preset": "0.1.0",
|
"entgamers-panda-preset": "0.1.0",
|
||||||
"formik": "^2.4.5",
|
"formik": "^2.4.5",
|
||||||
"framer-motion": "^10.16.16",
|
"framer-motion": "^10.16.16",
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ import Typography from '@/components/ui/Typography'
|
|||||||
import FormGroup from '@/components/ui/form/FormGroup'
|
import FormGroup from '@/components/ui/form/FormGroup'
|
||||||
import Input from '@/components/ui/form/Input'
|
import Input from '@/components/ui/form/Input'
|
||||||
import TextArea from '@/components/ui/form/TextArea'
|
import TextArea from '@/components/ui/form/TextArea'
|
||||||
import { createTeamApply } from '@/services/frontend/teamApply'
|
|
||||||
import { css } from '@/styled-system/css'
|
import { css } from '@/styled-system/css'
|
||||||
import { type Alert as AlertType } from '@/types/feedback'
|
import { type Alert as AlertType } from '@/types/feedback'
|
||||||
import { type TeamApplyData } from '@/types/teamApply'
|
import { type TeamApplyData } from '@/types/teamApply'
|
||||||
@@ -29,9 +28,9 @@ const ApplyForm: FC = () => {
|
|||||||
message: '',
|
message: '',
|
||||||
role: 'administrator'
|
role: 'administrator'
|
||||||
},
|
},
|
||||||
onSubmit: async (values) => {
|
onSubmit: async (_values) => {
|
||||||
try {
|
try {
|
||||||
await createTeamApply(values)
|
// await createTeamApply(values)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (error instanceof AppwriteException) {
|
if (error instanceof AppwriteException) {
|
||||||
setAlert({
|
setAlert({
|
||||||
|
|||||||
@@ -1,9 +0,0 @@
|
|||||||
import { Client, Databases } from 'appwrite'
|
|
||||||
|
|
||||||
export const appwriteClient = new Client()
|
|
||||||
.setEndpoint(process.env.NEXT_PUBLIC_APPWRITE_ENDPOINT ?? '')
|
|
||||||
.setProject(process.env.NEXT_PUBLIC_APPWRITE_PROJECT ?? '')
|
|
||||||
|
|
||||||
export const databases = new Databases(appwriteClient)
|
|
||||||
|
|
||||||
export { ID } from 'appwrite'
|
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
import { Client, Databases, Teams } from 'node-appwrite'
|
|
||||||
|
|
||||||
export const clientNodeAppwrite = new Client()
|
|
||||||
.setEndpoint(process.env.NEXT_PUBLIC_APPWRITE_ENDPOINT ?? '')
|
|
||||||
.setProject(process.env.NEXT_PUBLIC_APPWRITE_PROJECT_ID ?? '')
|
|
||||||
.setKey(process.env.APPWRITE_API_KEY ?? '')
|
|
||||||
|
|
||||||
export const databases = new Databases(clientNodeAppwrite)
|
|
||||||
|
|
||||||
export const teams = new Teams(clientNodeAppwrite)
|
|
||||||
|
|
||||||
export { Permission, Role } from 'node-appwrite'
|
|
||||||
@@ -1,18 +0,0 @@
|
|||||||
import { databases } from '@/lib/nodeAppwrite'
|
|
||||||
|
|
||||||
export const DATABASE_NAME = 'entgamers-website'
|
|
||||||
export const DATABASE_ID = process.env.NEXT_PUBLIC_APPWRITE_DATABASE_ID ?? DATABASE_NAME
|
|
||||||
|
|
||||||
export const ensureDatabase = (() => {
|
|
||||||
let databaseEnsured = false
|
|
||||||
return async () => {
|
|
||||||
if (databaseEnsured) return
|
|
||||||
try {
|
|
||||||
await databases.get(DATABASE_ID)
|
|
||||||
} catch (error) {
|
|
||||||
await databases.create(DATABASE_ID, DATABASE_NAME)
|
|
||||||
} finally {
|
|
||||||
databaseEnsured = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})()
|
|
||||||
@@ -1,35 +0,0 @@
|
|||||||
import { teams } from '@/lib/nodeAppwrite'
|
|
||||||
import { type UserPreferences } from '@/types/User'
|
|
||||||
|
|
||||||
export const ADMIN_TEAM_NAME = 'admin'
|
|
||||||
export const ADMIN_TEAM_ID = process.env.NEXT_PUBLIC_APPWRITE_ADMIN_TEAM_ID ?? ADMIN_TEAM_NAME
|
|
||||||
export const MODERATOR_TEAM_NAME = 'moderator'
|
|
||||||
export const MODERATOR_TEAM_ID = process.env.NEXT_PUBLIC_APPWRITE_MODERATOR_TEAM_ID ?? MODERATOR_TEAM_NAME
|
|
||||||
export const COLLABORATOR_TEAM_NAME = 'collaborator'
|
|
||||||
export const COLLABORATOR_TEAM_ID = process.env.NEXT_PUBLIC_APPWRITE_COLLABORATOR_TEAM_ID ?? COLLABORATOR_TEAM_NAME
|
|
||||||
|
|
||||||
const ensureRoleExists = (roleId: string, roleName: string): (() => Promise<void>) => {
|
|
||||||
let roleExists = false
|
|
||||||
return async (): Promise<void> => {
|
|
||||||
if (roleExists) return
|
|
||||||
try {
|
|
||||||
await teams.get<UserPreferences>(roleId)
|
|
||||||
roleExists = true
|
|
||||||
} catch (error) {
|
|
||||||
await teams.create(roleId, roleName)
|
|
||||||
roleExists = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export const ensureRoles = async (): Promise<void> => {
|
|
||||||
const adminRole = ensureRoleExists(ADMIN_TEAM_ID, ADMIN_TEAM_NAME)
|
|
||||||
const moderatorRole = ensureRoleExists(MODERATOR_TEAM_ID, MODERATOR_TEAM_NAME)
|
|
||||||
const collaboratorRole = ensureRoleExists(COLLABORATOR_TEAM_ID, COLLABORATOR_TEAM_NAME)
|
|
||||||
|
|
||||||
await Promise.all([
|
|
||||||
adminRole(),
|
|
||||||
moderatorRole(),
|
|
||||||
collaboratorRole()
|
|
||||||
])
|
|
||||||
}
|
|
||||||
@@ -1,32 +0,0 @@
|
|||||||
import { Permission, Role, databases } from '@/lib/nodeAppwrite'
|
|
||||||
import { DATABASE_ID, ensureDatabase } from '@/services/backend/database'
|
|
||||||
import { ADMIN_TEAM_ID } from './roles'
|
|
||||||
|
|
||||||
export const TEAM_APPLY_COLLECTION_NAME = 'team-apply'
|
|
||||||
export const TEAM_APPLY_COLLECTION_ID = process.env.NEXT_PUBLIC_APPWRITE_TEAM_APPLY_COLLECTION_ID ?? TEAM_APPLY_COLLECTION_NAME
|
|
||||||
|
|
||||||
export const ensureTeamApplyCollection = (() => {
|
|
||||||
let collectionEnsured = false
|
|
||||||
return async () => {
|
|
||||||
if (collectionEnsured) return
|
|
||||||
await ensureDatabase()
|
|
||||||
try {
|
|
||||||
await databases.getCollection(DATABASE_ID, TEAM_APPLY_COLLECTION_ID)
|
|
||||||
} catch (error) {
|
|
||||||
const permissions = [
|
|
||||||
Permission.create(Role.any()),
|
|
||||||
Permission.read(Role.team(ADMIN_TEAM_ID)),
|
|
||||||
Permission.update(Role.team(ADMIN_TEAM_ID)),
|
|
||||||
Permission.delete(Role.team(ADMIN_TEAM_ID))
|
|
||||||
]
|
|
||||||
await databases.createCollection(DATABASE_ID, TEAM_APPLY_COLLECTION_ID, TEAM_APPLY_COLLECTION_NAME, permissions, false, true)
|
|
||||||
await databases.createStringAttribute(DATABASE_ID, TEAM_APPLY_COLLECTION_ID, 'name', 128, true)
|
|
||||||
await databases.createEmailAttribute(DATABASE_ID, TEAM_APPLY_COLLECTION_ID, 'email', true)
|
|
||||||
await databases.createStringAttribute(DATABASE_ID, TEAM_APPLY_COLLECTION_ID, 'discordName', 128, true)
|
|
||||||
await databases.createStringAttribute(DATABASE_ID, TEAM_APPLY_COLLECTION_ID, 'role', 24, true)
|
|
||||||
await databases.createStringAttribute(DATABASE_ID, TEAM_APPLY_COLLECTION_ID, 'message', 4096, true)
|
|
||||||
} finally {
|
|
||||||
collectionEnsured = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})()
|
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
export const DATABASE_NAME = 'entgamers-website'
|
|
||||||
export const DATABASE_ID = process.env.NEXT_PUBLIC_APPWRITE_DATABASE_ID ?? DATABASE_NAME
|
|
||||||
@@ -1,25 +0,0 @@
|
|||||||
import { ID, databases } from '@/lib/appwrite'
|
|
||||||
import { DATABASE_ID } from '@/services/frontend/database'
|
|
||||||
import { type TeamApplyData, type TeamApplyDocument, type TeamApplyList } from '@/types/teamApply'
|
|
||||||
|
|
||||||
export const TEAM_APPLY_COLLECTION_NAME = 'team-apply'
|
|
||||||
export const TEAM_APPLY_COLLECTION_ID = process.env.NEXT_PUBLIC_APPWRITE_TEAM_APPLY_COLLECTION_ID ?? TEAM_APPLY_COLLECTION_NAME
|
|
||||||
|
|
||||||
export const createTeamApply = async (data: TeamApplyData): Promise<TeamApplyDocument> => {
|
|
||||||
const createdTeamApply = await databases.createDocument<TeamApplyDocument>(DATABASE_ID, TEAM_APPLY_COLLECTION_ID, ID.unique(), data)
|
|
||||||
return createdTeamApply
|
|
||||||
}
|
|
||||||
|
|
||||||
export const getTeamApply = async (teamApplyId: string): Promise<TeamApplyDocument> => {
|
|
||||||
const teamApply = await databases.getDocument<TeamApplyDocument>(DATABASE_ID, TEAM_APPLY_COLLECTION_ID, teamApplyId)
|
|
||||||
return teamApply
|
|
||||||
}
|
|
||||||
|
|
||||||
export const listTeamApply = async (): Promise<TeamApplyList> => {
|
|
||||||
const teamApplyList = await databases.listDocuments<TeamApplyDocument>(DATABASE_ID, TEAM_APPLY_COLLECTION_ID)
|
|
||||||
return teamApplyList
|
|
||||||
}
|
|
||||||
|
|
||||||
export const deleteTeamApply = async (teamApplyId: string): Promise<void> => {
|
|
||||||
await databases.deleteDocument(DATABASE_ID, TEAM_APPLY_COLLECTION_ID, teamApplyId)
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user