// Copyright (c) 2023 Sourcefuse Technologies // // This software is released under the MIT License. // https://opensource.org/licenses/MIT import { Count, CountSchema, Filter, repository, Where, } from '@loopback/repository'; import { del, get, getWhereSchemaFor, param, patch, post, requestBody, } from '@loopback/rest'; import { CONTENT_TYPE, getModelSchemaRefSF, OPERATION_SECURITY_SPEC, STATUS_CODE, } from '@sourceloop/core'; import {authenticate, STRATEGY} from 'loopback4-authentication'; import {authorize} from 'loopback4-authorization'; import {Calendar, WorkingHour} from '../models'; import {PermissionKey} from '../models/enums/permission-key.enum'; import {CalendarRepository} from '../repositories'; const basePath = '/calendars/{id}/working-hours'; export class CalendarWorkingHourController { constructor( @repository(CalendarRepository) protected calendarRepository: CalendarRepository, ) {} @authenticate(STRATEGY.BEARER, { passReqToCallback: true, }) @authorize({ permissions: [ PermissionKey.ViewWorkingHour, PermissionKey.ViewWorkingHourNum, ], }) @get(basePath, { security: OPERATION_SECURITY_SPEC, responses: { [STATUS_CODE.OK]: { description: 'Array of Calendar has many WorkingHour', content: { [CONTENT_TYPE.JSON]: { schema: {type: 'array', items: getModelSchemaRefSF(WorkingHour)}, }, }, }, }, }) async find( @param.path.string('id') id: string, @param.query.object('filter') filter?: Filter, ): Promise { return this.calendarRepository.workingHours(id).find(filter); } @authenticate(STRATEGY.BEARER, { passReqToCallback: true, }) @authorize({ permissions: [ PermissionKey.CreateWorkingHour, PermissionKey.CreateWorkingHourNum, ], }) @post(basePath, { description: 'This is an api to create a calendar for any user.', security: OPERATION_SECURITY_SPEC, responses: { [STATUS_CODE.OK]: { description: 'Calendar model instance', content: { [CONTENT_TYPE.JSON]: {schema: getModelSchemaRefSF(WorkingHour)}, }, }, }, }) async create( @param.path.string('id') id: typeof Calendar.prototype.id, @requestBody({ content: { [CONTENT_TYPE.JSON]: { schema: getModelSchemaRefSF(WorkingHour, { title: 'NewWorkingHourInCalendar', exclude: ['id'], optional: ['calendarId'], }), }, }, }) workingHour: Omit, ): Promise { return this.calendarRepository.workingHours(id).create(workingHour); } @authenticate(STRATEGY.BEARER, { passReqToCallback: true, }) @authorize({ permissions: [ PermissionKey.UpdateWorkingHour, PermissionKey.UpdateWorkingHourNum, ], }) @patch(basePath, { security: OPERATION_SECURITY_SPEC, responses: { [STATUS_CODE.OK]: { description: 'Calendar.WorkingHour PATCH success count', content: {[CONTENT_TYPE.JSON]: {schema: CountSchema}}, }, }, }) async patch( @param.path.string('id') id: string, @requestBody({ content: { [CONTENT_TYPE.JSON]: { schema: getModelSchemaRefSF(WorkingHour, {partial: true}), }, }, }) workingHour: Partial, @param.query.object('where', getWhereSchemaFor(WorkingHour)) where?: Where, ): Promise { return this.calendarRepository.workingHours(id).patch(workingHour, where); } @authenticate(STRATEGY.BEARER, { passReqToCallback: true, }) @authorize({ permissions: [ PermissionKey.DeleteWorkingHour, PermissionKey.DeleteWorkingHourNum, ], }) @del(basePath, { security: OPERATION_SECURITY_SPEC, responses: { [STATUS_CODE.OK]: { description: 'Calendar.WorkingHour DELETE success count', content: {[CONTENT_TYPE.JSON]: {schema: CountSchema}}, }, }, }) async delete( @param.path.string('id') id: string, @param.query.object('where', getWhereSchemaFor(WorkingHour)) where?: Where, ): Promise { return this.calendarRepository.workingHours(id).delete(where); } }