Hi I would like to know if it's possible to make a findOne or to call an action from the service with a decorator to have the full entity with the id from the url and then to give it to a guard for permission. I let you screen so you can understand what I want to do more clearly
#Make a findOne with id sent in url before a Guard
6 messages · Page 1 of 1 (latest)
Suggestion for @hexed mountain:
:warning: Please do not screenshot code as it causes a number of issues:
- Ease of assistance: if someone wants to copy your code and correct it, they cannot. Making it easy for people to help you is in your best interests.
- Editorializing: it's common to try to make images small, which means you're likely to crop out code relevant to your issue
- Accessibility: wide images can be hard to read on mobile devices, and are impossible for screen readers.
- Legibility: you cannot read screenshots of code directly, instead you have to open them in an enlarged context.
- Bandwidth usage/clutter: some of our members use metered connections, and it is wasteful for them to download images of a text.
For a small amount of code, please use a code-block.
/*
Here I want a @GetCurrentWorkTime() decorator to get the current work time and send it to AbilitieGuard
But for it I dont have CurrentWorkTimeId but I have the UserLinkId that I use to get the CurrentWorkTimeId in the service
*/
@CheckAbilities({
action: Action.Create,
subject: WorkTime,
match: {
elementToCheck: 'userLinkId',
},
})
@UseGuards(JwtAuthGuard, AbilitiesGuard)
@Post('start/user-link/:userLinkId')
async startAWorkTimeDay(
@Param('userLinkId') userLinkId: string,
@Body() createCurrentWorkTimeDto: CreateCurrentWorkTimeDto,
) {
return plainToInstance(
CurrentWorkTimeDto,
await this.workTimesService.startAWorkTimeDay(
createCurrentWorkTimeDto,
+userLinkId,
),
)
}
async startAWorkTimeDay(
createCurrentWorkTimeDto: CreateCurrentWorkTimeDto,
userLinkId: number,
) {
const currentWorkTime = await this._workTimeRepository.findOneBy({
userLinkId: userLinkId,
startTime: Not(IsNull()),
endTime: IsNull(),
})
if (currentWorkTime)
throw new HttpException(
'A current work time is already running',
409,
)
const now = new Date()
const time = now.getHours() + ':' + now.getMinutes() + ':00'
const savedWorkTime = await this._workTimeRepository.save(
plainToInstance(WorkTime, {
userLinkId: userLinkId,
delimitedDate: createCurrentWorkTimeDto.delimitedDate,
responsibleLink: createCurrentWorkTimeDto.responsibleLink,
startTime: time,
}),
)
return await this._workTimeRepository.findOneBy({
id: savedWorkTime.id,
})
}
If one day that can help someone I found this solution:
import { CanActivate, ExecutionContext, Injectable } from '@nestjs/common'
import { WorkTimesService } from './work-times.service'
/**
* @description This guard is used to check if the current userLinkId has a work time in progress and then to get it so we can check if the user have access to this work time
* @date 11/04/2024 - 12:57:05
*
* @export
* @class CurrentWorkTimeGuard
* @typedef {CurrentWorkTimeGuard}
* @implements {CanActivate}
*/
@Injectable()
export class CurrentWorkTimeGuard implements CanActivate {
constructor(private _workTimesService: WorkTimesService) {}
async canActivate(context: ExecutionContext): Promise<boolean> {
const request = context.switchToHttp().getRequest()
const { userLinkId } = request.params
const workTime =
await this._workTimesService.getCurrentWorkTime(userLinkId)
if (!workTime) {
return false
}
request.params.workTimeId = workTime.id
return true
}
}