Skip to content

Commit

Permalink
add features for watched directories
Browse files Browse the repository at this point in the history
  • Loading branch information
JamieVangeysel committed Dec 13, 2024
1 parent c60b188 commit 2b62e76
Show file tree
Hide file tree
Showing 2 changed files with 157 additions and 3 deletions.
100 changes: 97 additions & 3 deletions src/controllers/directories.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
import { FastifyInstance, FastifyRequest, FastifyReply } from 'fastify'

// Internal deps
import { IRepositoryBooleanResult } from "../repositories"
import Directory from "../repositories/directory.repository"

import { IRepositoryBooleanResult } from '../repositories'
import Directory from '../repositories/directory.repository'
export default async function (fastify: FastifyInstance) {
/**
* @route GET /{APP_VERSION}/directories
Expand Down Expand Up @@ -144,4 +143,99 @@ export default async function (fastify: FastifyInstance) {
return reply.error('failed to delete directory!')
}
})


/**
* Watched directories routes
*/
fastify.get('/watched', async function (request: FastifyRequest<{
Params: { id: number }
}>, reply: FastifyReply) {
const start = performance.now()

if (!request.jwt?.sub)
return reply.fail({ jwt: 'missing authorization' }, 401)

if (!request.hasPermission('read', 'GroupClaes.PCM/directory/watch'))
return reply.fail({ role: 'missing permission' }, 403)


try {
const pool = await fastify.getSqlPool()
const repo = new Directory(request.log, pool)
const result = await repo.getWatched(request.params.id, request.jwt.sub)

if (result.verified) {
if (result.error) return reply.error(result.error)

return reply.success({ directories: result.result }, 200, performance.now() - start)
}

return reply.error('Session has expired!', 401, performance.now() - start)
} catch (err) {
request.log.error({ err }, 'failed to watch directory!')
return reply.error('failed to watch directory!')
}
})
fastify.put('/:id/watch', async function (request: FastifyRequest<{
Params: { id: number }
}>, reply: FastifyReply) {
const start = performance.now()

if (!request.jwt?.sub)
return reply.fail({ jwt: 'missing authorization' }, 401)

if (!request.hasPermission('write', 'GroupClaes.PCM/directory/watch'))
return reply.fail({ role: 'missing permission' }, 403)

try {
const pool = await fastify.getSqlPool()
const repo = new Directory(request.log, pool)
const result = await repo.watch(request.params.id, request.jwt.sub)

if (result.verified) {
if (result.error) return reply.error(result.error)

return reply.success(null, 200, performance.now() - start)
}

return reply.error('Session has expired!', 401, performance.now() - start)
} catch (err) {
request.log.error({ err }, 'failed to watch directory!')
return reply.error('failed to watch directory!')
}
})


/**
* Watched directories routes
*/
fastify.put('/:id/unwatch', async function (request: FastifyRequest<{
Params: { id: number }
}>, reply: FastifyReply) {
const start = performance.now()

if (!request.jwt?.sub)
return reply.fail({ jwt: 'missing authorization' }, 401)

if (!request.hasPermission('write', 'GroupClaes.PCM/directory/watch'))
return reply.fail({ role: 'missing permission' }, 403)

try {
const pool = await fastify.getSqlPool()
const repo = new Directory(request.log, pool)
const result = await repo.unwatch(request.params.id, request.jwt.sub)

if (result.verified) {
if (result.error) return reply.error(result.error)

return reply.success(null, 200, performance.now() - start)
}

return reply.error('Session has expired!', 401, performance.now() - start)
} catch (err) {
request.log.error({ err }, 'failed to unwatch directory!')
return reply.error('failed to unwatch directory!')
}
})
}
60 changes: 60 additions & 0 deletions src/repositories/document.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,66 @@ export default class Document {
const { path } = result.recordset[0]
return path
}

async getWatched(directory_id: number, user_id?: string): Promise<DBResultSet> {
const r = new sql.Request(this._pool)
r.input('user_id', sql.Int, user_id)
r.input('directory_id', sql.Int, directory_id)

const result = await r.execute(`${this.schema}uspGetWatchedDirectories`)

const { error, verified } = result.recordset[0]

if (!error) {
return {
error,
verified,
result: result.recordsets[1] || []
}
} else {
throw new Error(error)
}
}

async watch(directory_id: number, user_id?: string): Promise<DBResultSet> {
const r = new sql.Request(this._pool)
r.input('user_id', sql.Int, user_id)
r.input('directory_id', sql.Int, directory_id)

const result = await r.execute(`${this.schema}uspAddWatchedDirecotry`)

const { error, verified } = result.recordset[0]

if (!error) {
return {
error,
verified,
result: result.recordsets[1][0] || false
}
} else {
throw new Error(error)
}
}

async unwatch(directory_id: number, user_id?: string): Promise<DBResultSet> {
const r = new sql.Request(this._pool)
r.input('user_id', sql.Int, user_id)
r.input('directory_id', sql.Int, directory_id)

const result = await r.execute(`${this.schema}uspRemoveWatchedDirectory`)

const { error, verified } = result.recordset[0]

if (!error) {
return {
error,
verified,
result: result.recordsets[1][0] || false
}
} else {
throw new Error(error)
}
}
}

export interface DBResultSet {
Expand Down

0 comments on commit 2b62e76

Please sign in to comment.