Skip to content

Commit

Permalink
Merge pull request #25 from CodeCrowCorp/dev
Browse files Browse the repository at this point in the history
Feat: added Admin API endpoints
  • Loading branch information
gagansuie authored Dec 8, 2022
2 parents a687704 + 2b452f5 commit 6af18e2
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 28 deletions.
54 changes: 27 additions & 27 deletions src/lib/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,43 @@ import { browser, dev } from '$app/environment'
import type { FirebaseApp, FirebaseOptions } from 'firebase/app'
import { readable } from 'svelte/store'
import {
PUBLIC_FIREBASE_API_KEY,
PUBLIC_FIREBASE_AUTH_DOMAIN,
PUBLIC_FIREBASE_PROJECT_ID,
PUBLIC_FIREBASE_STORAGE_BUCKET,
PUBLIC_FIREBASE_MESSAGE_SENDER_ID,
PUBLIC_FIREBASE_APP_ID,
PUBLIC_FIREBASE_API_KEY,
PUBLIC_FIREBASE_AUTH_DOMAIN,
PUBLIC_FIREBASE_PROJECT_ID,
PUBLIC_FIREBASE_STORAGE_BUCKET,
PUBLIC_FIREBASE_MESSAGE_SENDER_ID,
PUBLIC_FIREBASE_APP_ID,
} from '$env/static/public'

const firebaseConfig: FirebaseOptions = dev
? { apiKey: 'demo', authDomain: 'demo.firebaseapp.com' }
: {
apiKey: PUBLIC_FIREBASE_API_KEY,
authDomain: PUBLIC_FIREBASE_AUTH_DOMAIN,
projectId: PUBLIC_FIREBASE_PROJECT_ID,
storageBucket: PUBLIC_FIREBASE_STORAGE_BUCKET,
messagingSenderId: PUBLIC_FIREBASE_MESSAGE_SENDER_ID,
appId: PUBLIC_FIREBASE_APP_ID,
}
? { apiKey: 'demo', authDomain: 'demo.firebaseapp.com' }
: {
apiKey: PUBLIC_FIREBASE_API_KEY,
authDomain: PUBLIC_FIREBASE_AUTH_DOMAIN,
projectId: PUBLIC_FIREBASE_PROJECT_ID,
storageBucket: PUBLIC_FIREBASE_STORAGE_BUCKET,
messagingSenderId: PUBLIC_FIREBASE_MESSAGE_SENDER_ID,
appId: PUBLIC_FIREBASE_APP_ID,
}

// load the firebase app on demand by putting it in a store
// this can then be used in derived stores for auth, firestore, and other services
function createApp() {
let app: FirebaseApp
let app: FirebaseApp

const { subscribe } = readable<FirebaseApp>(undefined, (set) => {
async function init() {
if (!app) {
const { initializeApp } = await import('firebase/app')
app = initializeApp(firebaseConfig)
}
set(app)
}
const { subscribe } = readable<FirebaseApp>(undefined, (set) => {
async function init() {
if (!app) {
const { initializeApp } = await import('firebase/app')
app = initializeApp(firebaseConfig)
}
set(app)
}

if (browser) init()
})
if (browser) init()
})

return { subscribe }
return { subscribe }
}

export const app = createApp()
112 changes: 111 additions & 1 deletion src/stores/adminStore.ts
Original file line number Diff line number Diff line change
@@ -1 +1,111 @@
import { writable } from 'svelte/store'
import { PUBLIC_API_URL } from '$env/static/public'

class AdminStore {
public async fetchAdmins({ id }: { id: string }) {
return await fetch(`${PUBLIC_API_URL}/admin/${id}`, {
method: 'POST'
}).then(response => response.json())
}

public async uploadFile({ file, url }: { file: File, url: string }) {
return await fetch(url, {
method: 'PUT',
headers: {
'x-amz-acl': 'public-read',
'Content-Type': file.type
}, //reportProgress: true, observe: 'events',
body: JSON.stringify(file)
}).then(response => response.json()).catch(err => console.log('err', err))
}

public async getUserRole() {
return await fetch(`${PUBLIC_API_URL}/roles/role-mapping`, {
method: 'GET'
}).then(response => response.json())
}

public async getRoles() {
return await fetch(`${PUBLIC_API_URL}/roles`, {
method: 'GET'
}).then(response => response.json())
}

public async getAdmins({ roleId }: { roleId: string }) {
return await fetch(`${PUBLIC_API_URL}/roles/users?roleId=${roleId}`, {
method: 'GET'
}).then(response => response.json())
}

public async addAdmin({ userId, roleId }: { userId: string, roleId: string }) {
return await fetch(`${PUBLIC_API_URL}/roles/role-mapping`, {
method: 'POST',
body: JSON.stringify({ roleId, userId })
}).then(response => response.json())
}

public async removeAdmin({ userId, roleId }: { userId: string, roleId: string }) {
return await fetch(`${PUBLIC_API_URL}/roles/role-mapping`, {
method: 'PATCH',
body: JSON.stringify({ roleId, userId })
}).then(response => response.json())
}

public async getUploadURL({ fileName, fileType, bucketName }: { fileName: string, fileType: string, bucketName: string }) {
return await fetch(`${PUBLIC_API_URL}/attachments/url`, {
method: 'PUT',
body: JSON.stringify({ fileName, fileType, bucketName })
}).then(response => response.json())
}

public async getVideos({ filter = '', sortOrder = 'asc', pageNumber = 0, pageSize = 5 }: { filter: string, sortOrder: string, pageNumber: number, pageSize: number }) {
return await fetch(`${PUBLIC_API_URL}/videos?admin=1&filter=${filter}&skip=${pageNumber}&limit=${pageSize}&sort=${sortOrder}`, {
method: 'GET'
}).then(response => response.json())
}

public async getAllVideos() {
return await fetch(`${PUBLIC_API_URL}/videos/all?admin=1`, {
method: 'GET'
}).then(response => response.json())
}

public async getChannels() {
return await fetch(`${PUBLIC_API_URL}/channels`, {
method: 'GET'
}).then(response => response.json())
}

public async getChannelLiveStreams({ id }: { id: string }) {
return await fetch(`${PUBLIC_API_URL}/channels/live-streams?channelId=${id}`, {
method: 'GET'
}).then(response => response.json())
}

public async getMonthLiveStreaming() {
return await fetch(`${PUBLIC_API_URL}/streams`, {
method: 'GET'
}).then(response => response.json())
}

public async createLegalDoc({ title, createdAt, pdf }: { title: string, createdAt: string, pdf: string }) {
return await fetch(`${PUBLIC_API_URL}/legal`, {
method: 'POST',
body: JSON.stringify({ title, createdAt, pdf })
}).then(response => response.json())
}

public async getLegalDocs({ bucketName }: { bucketName: string }) {
return await fetch(`${PUBLIC_API_URL}/legal/get/objects?bucketName=${bucketName}`, {
method: 'GET',
}).then(response => response.json())
}

public async setUserBan({ id, isBanned }: { id: string, isBanned: string }) {
return await fetch(`${PUBLIC_API_URL}/users/ban?userId=${id}`, {
method: 'PATCH',
body: JSON.stringify({ isBanned })
}).then(response => response.json())
}
}

export const adminStore = new AdminStore()

0 comments on commit 6af18e2

Please sign in to comment.