Skip to content

Commit

Permalink
Enable "working-hours" module
Browse files Browse the repository at this point in the history
  • Loading branch information
ba1uev committed Sep 25, 2024
1 parent edcaf08 commit d96e5f9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,6 @@ export const cronJob: CronJob = {
id: { [Op.notIn]: excludedUserIds },
roles: { [Op.overlap]: allowedRoles },
isInitialised: true,
// NOTE: test mode
email: { [Op.in]: config.workingHoursTestGroup },
},
})

Expand Down
64 changes: 36 additions & 28 deletions src/modules/working-hours/server/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,26 +47,25 @@ import { WorkingHoursUserConfig } from './models'

dayjs.extend(dayjsIsoWeek)

function isOutOfTestGroup(user: User): boolean {
return !config.workingHoursTestGroup.includes(user.email)
const MODULE_METADATA = appConfig.getModuleMetadata('working-hours') as Metadata

function getTargetRole(user: User): string | null {
return (
Object.keys(MODULE_METADATA.configByRole).find(fp.isIn(user.roles)) || null
)
}

const userRouter: FastifyPluginCallback = async function (fastify, opts) {
fastify.get('/config', async (req: FastifyRequest, reply) => {
if (!req.can(Permissions.Create)) {
return null
}
if (isOutOfTestGroup(req.user)) {
return null
}
const metadata = appConfig.getModuleMetadata('working-hours') as Metadata
if (!metadata) return null
if (!MODULE_METADATA) return null

const allowedRoles = Object.keys(metadata.configByRole)
const userRole = req.user.roles.find((x) => allowedRoles.includes(x))
const userRole = getTargetRole(req.user)
if (!userRole) return null

const roleConfig = metadata.configByRole[userRole] || null
const roleConfig = MODULE_METADATA.configByRole[userRole] || null
if (!roleConfig) return null
const result: WorkingHoursConfig = {
...roleConfig,
Expand Down Expand Up @@ -103,7 +102,7 @@ const userRouter: FastifyPluginCallback = async function (fastify, opts) {
reply
) => {
req.check(Permissions.Create)
if (isOutOfTestGroup(req.user)) {
if (!getTargetRole(req.user)) {
return reply.throw.accessDenied()
}
const entries = await fastify.db.WorkingHoursEntry.findAll({
Expand All @@ -129,7 +128,7 @@ const userRouter: FastifyPluginCallback = async function (fastify, opts) {
reply
) => {
req.check(Permissions.Create)
if (isOutOfTestGroup(req.user)) {
if (!getTargetRole(req.user)) {
return reply.throw.accessDenied()
}
const startDate = dayjs(req.query.startDate, DATE_FORMAT)
Expand All @@ -154,7 +153,7 @@ const userRouter: FastifyPluginCallback = async function (fastify, opts) {
reply
) => {
req.check(Permissions.Create)
if (isOutOfTestGroup(req.user)) {
if (!getTargetRole(req.user)) {
return reply.throw.accessDenied()
}
const roleConfig = getModuleRoleConfig(req.user.roles, appConfig)
Expand Down Expand Up @@ -182,7 +181,7 @@ const userRouter: FastifyPluginCallback = async function (fastify, opts) {
reply
) => {
req.check(Permissions.Create)
if (isOutOfTestGroup(req.user)) {
if (!getTargetRole(req.user)) {
return reply.throw.accessDenied()
}
const newEntries: Array<
Expand Down Expand Up @@ -231,6 +230,9 @@ const userRouter: FastifyPluginCallback = async function (fastify, opts) {
'/entries/:entryId',
async (req: FastifyRequest<{ Params: { entryId: string } }>, reply) => {
req.check(Permissions.Create)
if (!getTargetRole(req.user)) {
return reply.throw.accessDenied()
}
const entry = await fastify.db.WorkingHoursEntry.findOne({
where: {
userId: req.user.id,
Expand All @@ -255,6 +257,9 @@ const userRouter: FastifyPluginCallback = async function (fastify, opts) {
reply
) => {
req.check(Permissions.Create)
if (!getTargetRole(req.user)) {
return reply.throw.accessDenied()
}
const entry = await fastify.db.WorkingHoursEntry.findOne({
where: {
userId: req.user.id,
Expand Down Expand Up @@ -301,7 +306,7 @@ const userRouter: FastifyPluginCallback = async function (fastify, opts) {

fastify.get('/default-entries', async (req, reply) => {
req.check(Permissions.Create)
if (isOutOfTestGroup(req.user)) {
if (!getTargetRole(req.user)) {
return reply.throw.accessDenied()
}
const entries = await fastify.db.DefaultWorkingHoursEntry.findAll({
Expand All @@ -319,7 +324,7 @@ const userRouter: FastifyPluginCallback = async function (fastify, opts) {
reply
) => {
req.check(Permissions.Create)
if (isOutOfTestGroup(req.user)) {
if (!getTargetRole(req.user)) {
return reply.throw.accessDenied()
}
const newEntry = {
Expand Down Expand Up @@ -355,6 +360,9 @@ const userRouter: FastifyPluginCallback = async function (fastify, opts) {
'/default-entries/:entryId',
async (req: FastifyRequest<{ Params: { entryId: string } }>, reply) => {
req.check(Permissions.Create)
if (!getTargetRole(req.user)) {
return reply.throw.accessDenied()
}
const entry = await fastify.db.DefaultWorkingHoursEntry.findOne({
where: {
userId: req.user.id,
Expand All @@ -379,6 +387,9 @@ const userRouter: FastifyPluginCallback = async function (fastify, opts) {
reply
) => {
req.check(Permissions.Create)
if (!getTargetRole(req.user)) {
return reply.throw.accessDenied()
}
const entry = await fastify.db.DefaultWorkingHoursEntry.findOne({
where: {
userId: req.user.id,
Expand Down Expand Up @@ -428,8 +439,7 @@ const userRouter: FastifyPluginCallback = async function (fastify, opts) {
const adminRouter: FastifyPluginCallback = async function (fastify, opts) {
fastify.get('/config', async (req: FastifyRequest, reply) => {
req.check(Permissions.AdminList)
const metadata = appConfig.getModuleMetadata('working-hours') as Metadata
return metadata.configByRole
return MODULE_METADATA.configByRole
})

fastify.get(
Expand Down Expand Up @@ -541,9 +551,7 @@ const adminRouter: FastifyPluginCallback = async function (fastify, opts) {
reply
) => {
req.check(Permissions.AdminList)
const metadata = appConfig.getModuleMetadata('working-hours') as Metadata
const allowedRoles = Object.keys(metadata.configByRole)

const allowedRoles = Object.keys(MODULE_METADATA.configByRole)
const where: WhereOptions<WorkingHoursUserConfig> = {}
if (req.query.userId) {
where['userId'] = req.query.userId
Expand Down Expand Up @@ -582,15 +590,14 @@ const adminRouter: FastifyPluginCallback = async function (fastify, opts) {
if (!req.query.role || !req.query.from || !req.query.to) {
return reply.throw.badParams('Missing parameters')
}
const metadata = appConfig.getModuleMetadata('working-hours') as Metadata
if (
!metadata ||
!MODULE_METADATA ||
!req.query.role ||
!metadata.configByRole[req.query.role]
!MODULE_METADATA.configByRole[req.query.role]
) {
return reply.throw.misconfigured('Unsuported role')
}
const moduleConfig = metadata.configByRole[
const moduleConfig = MODULE_METADATA.configByRole[
req.query.role
] as WorkingHoursConfig

Expand Down Expand Up @@ -867,13 +874,14 @@ const adminRouter: FastifyPluginCallback = async function (fastify, opts) {
return reply.throw.notFound()
}

const metadata = appConfig.getModuleMetadata('working-hours') as Metadata
const allowedRoles = Object.keys(metadata.configByRole)
const allowedRoles = Object.keys(MODULE_METADATA.configByRole)
const userRole = user.roles.find((x) => allowedRoles.includes(x))
if (!userRole) {
return reply.throw.misconfigured('Unsuported role')
}
const moduleConfig = metadata.configByRole[userRole] as WorkingHoursConfig
const moduleConfig = MODULE_METADATA.configByRole[
userRole
] as WorkingHoursConfig

const userConfig = await fastify.db.WorkingHoursUserConfig.findOne({
where: { userId: user.id },
Expand Down

0 comments on commit d96e5f9

Please sign in to comment.