Skip to content

Commit

Permalink
Merge pull request #117 from CodeCrowCorp/dev
Browse files Browse the repository at this point in the history
Chore: changed export classes to
  • Loading branch information
gagansuie authored Jan 15, 2023
2 parents a4996cc + 4dbb68b commit 7c163da
Show file tree
Hide file tree
Showing 8 changed files with 379 additions and 354 deletions.
216 changes: 115 additions & 101 deletions src/lib/stores/adminStore.ts
Original file line number Diff line number Diff line change
@@ -1,105 +1,119 @@
import { env } from '$env/dynamic/public'

class AdminStore {
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(`${env.PUBLIC_API_URL}/roles/role-mapping`, {
method: 'GET'
}).then(response => response.json())
}

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

public async getAdmins({ roleId }: { roleId: string }) {
return await fetch(`${env.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(`${env.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(`${env.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(`${env.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(`${env.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(`${env.PUBLIC_API_URL}/videos/all?admin=1`, {
method: 'GET'
}).then(response => response.json())
}

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

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

public async getMonthLiveStreaming() {
return await fetch(`${env.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(`${env.PUBLIC_API_URL}/legal`, {
method: 'POST',
body: JSON.stringify({ title, createdAt, pdf })
}).then(response => response.json())
}

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

public async setUserBan({ id, isBanned }: { id: string, isBanned: string }) {
return await fetch(`${env.PUBLIC_API_URL}/users/ban?userId=${id}`, {
method: 'PATCH',
body: JSON.stringify({ isBanned })
}).then(response => response.json())
}
async function 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))
}

export const adminStore = new AdminStore()
async function getUserRole() {
return await fetch(`${env.PUBLIC_API_URL}/roles/role-mapping`, {
method: 'GET'
}).then(response => response.json())
}

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

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

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

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

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

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

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

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

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

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

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

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

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

export {
uploadFile,
getUserRole,
getRoles,
getAdmins,
addAdmin,
removeAdmin,
getUploadURL,
getVideos,
getAllVideos,
getChannels,
getChannelLiveStreams,
getMonthLiveStreaming,
createLegalDoc,
getLegalDocs,
setUserBan
}
101 changes: 47 additions & 54 deletions src/lib/stores/authStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,64 +2,57 @@ import { env } from '$env/dynamic/public'
import { writable, type Writable } from 'svelte/store'
const JWT_KEY = 'jwt'

class AuthStore {
constructor(public currentUser: Writable<any> = writable(null)) {}
export const currentUser: Writable<any> = writable(null)

public logout() {
this.setUser({ user: null })
window.localStorage.clear()
window.location.href = '/logout'
}

public setJWT({ jwt }: { jwt: string }) {
window.localStorage.setItem(JWT_KEY, jwt)
}

public setUserId({ userId }: { userId: string }) {
window.localStorage.setItem('userId', userId)
}
function logout() {
setUser({ user: null })
window.localStorage.clear()
window.location.href = '/'
}

public getJWT() {
return window.localStorage.getItem(JWT_KEY)
}
function setJWT({ jwt }: { jwt: string }) {
window.localStorage.setItem(JWT_KEY, jwt)
}

public getUserId() {
return window.localStorage.getItem('userId')
}
function setUserId({ userId }: { userId: string }) {
window.localStorage.setItem('userId', userId)
}

public setUser({ user }: { user: any }) {
if (!user) {
window.localStorage.clear()
}
this.currentUser.set(user)
}
function setUser({ user }: { user: any }) {
if (!user) {
window.localStorage.clear()
}
currentUser.set(user)
}

public async me() {
const jwt = window.localStorage.getItem('jwt')
const userId = window.localStorage.getItem('jwt')
if (jwt === null || userId === null) {
this.logout()
return null
} else {
return await fetch(`${env.PUBLIC_API_URL}/auth/me`, {
method: 'GET',
headers: {
Authorization: jwt,
userId: userId
}
})
.then(async (response) => {
const res = await response.json()
this.setUser({ user: res.user })
if (res.freshJwt) this.setJWT({ jwt: res.freshJwt })
return res.user
})
.catch((err) => {
if (err.status === 401 || err.includes('Error')) this.logout()
return null
})
}
}
async function me() {
const jwt = window.localStorage.getItem('jwt')
const userId = window.localStorage.getItem('jwt')
if (jwt === null || userId === null) {
logout(); return null
} else {
return await fetch(`${env.PUBLIC_API_URL}/auth/me`, {
method: 'GET',
headers: {
'Authorization': jwt,
'userId': userId
}
}).then(async response => {
const res = await response.json()
setUser({ user: res.user })
if (res.freshJwt) setJWT({ jwt: res.freshJwt })
return res.user
}).catch((err) => {
if (err.status === 401 || err.includes('Error')) logout()
return null
})
}
}

export const authStore = new AuthStore()
export {
logout,
setJWT,
setUserId,
setUser,
me
}
4 changes: 3 additions & 1 deletion src/lib/stores/channelStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,4 +308,6 @@ class ChannelStore {
// }
}

export const channelStore = new ChannelStore()
export {

}
Loading

0 comments on commit 7c163da

Please sign in to comment.