Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
punitkr03 committed Oct 2, 2024
1 parent a4d3394 commit 62c77ee
Show file tree
Hide file tree
Showing 17 changed files with 270 additions and 315 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"extends": ["plugin:@typescript-eslint/recommended"],
"rules": {
"indent": ["error", 2],
"linebreak-style": ["error", "unix"],
"linebreak-style": ["error", "windows"],
"quotes": ["error", "single"],
"semi": ["error", "always"],
"@typescript-eslint/no-explicit-any": "off",
Expand Down
5 changes: 5 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"tabWidth": 2,
"useTabs": false,
"singleQuote": true
}
32 changes: 16 additions & 16 deletions src/authentication.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { ServiceAddons } from "@feathersjs/feathers";
import { AuthenticationService, JWTStrategy } from "@feathersjs/authentication";
import { LocalStrategy } from "@feathersjs/authentication-local";
import { expressOauth } from "@feathersjs/authentication-oauth";
import { ServiceAddons } from '@feathersjs/feathers';
import { AuthenticationService, JWTStrategy } from '@feathersjs/authentication';
import { LocalStrategy } from '@feathersjs/authentication-local';
import { expressOauth } from '@feathersjs/authentication-oauth';

import { Application } from "./declarations";
import { NotAuthenticated } from "@feathersjs/errors";
import { Application } from './declarations';
import { NotAuthenticated } from '@feathersjs/errors';

declare module "./declarations" {
declare module './declarations' {
interface ServiceTypes {
authentication: AuthenticationService & ServiceAddons<any>;
}
Expand All @@ -17,10 +17,10 @@ export default function (app: Application): void {

class CodeOrOTP extends LocalStrategy {
async comparePassword(user: any, code: any) {
const authCodeService = app.service("otp");
const authCodeService = app.service('otp');
const authCode = await authCodeService._find({
query: {
type: "email",
type: 'email',
dest: user.email,
$sort: {
createdAt: -1,
Expand All @@ -29,14 +29,14 @@ export default function (app: Application): void {
paginate: false,
});

if ((authCode[0] && authCode[0].otp === code) || code === "0000") {
if ((authCode[0] && authCode[0].otp === code) || code === '0000') {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
if (authCode[0]) await authCodeService._remove(authCode[0]._id);
return user;
}

throw new NotAuthenticated("Invalid OTP");
throw new NotAuthenticated('Invalid OTP');
}
}

Expand All @@ -50,10 +50,10 @@ export default function (app: Application): void {
}
}

authentication.register("jwt", new JWTStrategy());
authentication.register("local", new LocalStrategy());
authentication.register("local-email", new CodeOrOTP());
authentication.register("server-email", new ServerEmailOTP());
app.use("/authentication", authentication);
authentication.register('jwt', new JWTStrategy());
authentication.register('local', new LocalStrategy());
authentication.register('local-email', new CodeOrOTP());
authentication.register('server-email', new ServerEmailOTP());
app.use('/authentication', authentication);
app.configure(expressOauth());
}
26 changes: 13 additions & 13 deletions src/constants/setQuery.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { Hook, HookContext } from "@feathersjs/feathers";
import { Hook, HookContext } from '@feathersjs/feathers';

const setQuery =
(key: string, value: any): Hook =>
(context: HookContext): HookContext => {
console.log(context.params.user);
if (typeof context.params.provider === "undefined") return context;
(context: HookContext): HookContext => {
console.log(context.params.user);
if (typeof context.params.provider === 'undefined') return context;

if (!context.params.query) {
context.params.query = {};
}
context.params.query[key] = context.params.user
? context.params.user[value]
: undefined;
console.log(context.params.query);
return context;
};
if (!context.params.query) {
context.params.query = {};
}
context.params.query[key] = context.params.user
? context.params.user[value]
: undefined;
console.log(context.params.query);
return context;
};

export default setQuery;
16 changes: 8 additions & 8 deletions src/hooks/disallow-if-booked.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BadRequest, NotAuthenticated } from "@feathersjs/errors";
import { Hook, HookContext } from "@feathersjs/feathers";
import BookingStatus from "../constants/booking-status.enum";
import { BadRequest, NotAuthenticated } from '@feathersjs/errors';
import { Hook, HookContext } from '@feathersjs/feathers';
import BookingStatus from '../constants/booking-status.enum';

export default (): Hook => {
/**
Expand All @@ -19,22 +19,22 @@ export default (): Hook => {
if (!user) throw new NotAuthenticated();
if (!data.room || !data.dates)
throw new BadRequest(
"Please provide room and dates to create the booking."
'Please provide room and dates to create the booking.'
);

let { dates } = data;
const { dates } = data;
const { room } = data;
if (!Array.isArray(dates) || dates.length !== 2) {
throw new BadRequest(
"Please provide a valid date range with start and end dates."
'Please provide a valid date range with start and end dates.'
);
}
const [startDate, endDate] = dates.map(
(date: string | Date) => new Date(date)
);

try {
const existingBookings = await app.service("bookings").Model.find({
const existingBookings = await app.service('bookings').Model.find({
room,
dates: {
$elemMatch: {
Expand All @@ -49,7 +49,7 @@ export default (): Hook => {
console.log(existingBookings);
if (existingBookings.length > 0) {
throw new BadRequest(
"This room is already booked for the specified dates."
'This room is already booked for the specified dates.'
);
}

Expand Down
24 changes: 14 additions & 10 deletions src/models/bookings.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
// for more of what you can do here.
import BookingStatus, {
BookingStatusList,
} from "../constants/booking-status.enum";
import { Application } from "../declarations";
import { Model, Mongoose } from "mongoose";
} from '../constants/booking-status.enum';
import { Application } from '../declarations';
import { Model, Mongoose } from 'mongoose';

export default function (app: Application): Model<any> {
const modelName = "bookings";
const mongooseClient: Mongoose = app.get("mongooseClient");
const modelName = 'bookings';
const mongooseClient: Mongoose = app.get('mongooseClient');
const { Schema } = mongooseClient;
const { ObjectId } = Schema.Types;

Expand All @@ -25,12 +25,16 @@ export default function (app: Application): Model<any> {
* can only be done via SUPER_ADMIN
*/
type: ObjectId,
ref: "users",
ref: 'users',
required: true,
},
room: {
type: ObjectId,
ref: "rooms",
ref: 'rooms',
required: true,
},
roomNumber: {
type: String,
required: true,
},
dates: [
Expand All @@ -40,7 +44,7 @@ export default function (app: Application): Model<any> {
],
lastManagedBy: {
type: ObjectId,
ref: "users",
ref: 'users',
},
/**
* flag to determine whether the booking was done by superadmin,or not
Expand All @@ -60,7 +64,7 @@ export default function (app: Application): Model<any> {
},
createdBy: {
type: ObjectId,
ref: "users",
ref: 'users',
required: true,
},
deleted: {
Expand All @@ -70,7 +74,7 @@ export default function (app: Application): Model<any> {
},
deletedBy: {
type: ObjectId,
ref: "users",
ref: 'users',
},
deletedAt: {
type: Date,
Expand Down
14 changes: 7 additions & 7 deletions src/models/rooms.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
//
// See http://mongoosejs.com/docs/models.html
// for more of what you can do here.
import RoomStatusEnum, { RoomStatusList } from "../constants/room-status.enum";
import RoomStatusEnum, { RoomStatusList } from '../constants/room-status.enum';
// import RoomTypeEnum from "../constants/room-type.enum";
import { Application } from "../declarations";
import { Model, Mongoose } from "mongoose";
import { Application } from '../declarations';
import { Model, Mongoose } from 'mongoose';

export default function (app: Application): Model<any> {
const modelName = "rooms";
const mongooseClient: Mongoose = app.get("mongooseClient");
const modelName = 'rooms';
const mongooseClient: Mongoose = app.get('mongooseClient');
const { Schema } = mongooseClient;
const schema = new Schema(
{
Expand Down Expand Up @@ -54,7 +54,7 @@ export default function (app: Application): Model<any> {
],
createdBy: {
type: Schema.Types.ObjectId,
ref: "users",
ref: 'users',
required: true,
},
deleted: {
Expand All @@ -64,7 +64,7 @@ export default function (app: Application): Model<any> {
},
deletedBy: {
type: Schema.Types.ObjectId,
ref: "users",
ref: 'users',
},
deletedAt: {
type: Date,
Expand Down
20 changes: 10 additions & 10 deletions src/services/available-rooms/available-rooms.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import {
Paginated,
Params,
ServiceMethods,
} from "@feathersjs/feathers";
import { Application } from "../../declarations";
import { BadRequest } from "@feathersjs/errors";
} from '@feathersjs/feathers';
import { Application } from '../../declarations';
import { BadRequest } from '@feathersjs/errors';

interface Data {}

Expand Down Expand Up @@ -38,26 +38,26 @@ export class AvailableRooms implements ServiceMethods<Data> {
const { startDate, endDate } = params?.query as QueryInterface;

if (!startDate || !endDate)
throw new BadRequest("Please provide startDate and endDate");
throw new BadRequest('Please provide startDate and endDate');

const start = new Date(startDate);
const end = new Date(endDate);

if (isNaN(start.getTime()) || isNaN(end.getTime())) {
throw new BadRequest("Invalid date format");
throw new BadRequest('Invalid date format');
}

const roomsModel = this.app.service("rooms").Model;
const roomsModel = this.app.service('rooms').Model;

// rooms that are not booked during the specified dates
const roomsWithOrWithoutBookings = await roomsModel
.aggregate([
{
$lookup: {
from: "bookings",
localField: "_id",
foreignField: "room",
as: "bookings",
from: 'bookings',
localField: '_id',
foreignField: 'room',
as: 'bookings',
},
},
{
Expand Down
11 changes: 5 additions & 6 deletions src/services/available-rooms/available-rooms.hooks.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import { HooksObject } from '@feathersjs/feathers';
import * as authentication from '@feathersjs/authentication';
// Don't remove this comment. It's needed to format import lines nicely.

const { authenticate } = authentication.hooks;

export default {
before: {
all: [ authenticate('jwt') ],
all: [authenticate('jwt')],
find: [],
get: [],
create: [],
update: [],
patch: [],
remove: []
remove: [],
},

after: {
Expand All @@ -22,7 +21,7 @@ export default {
create: [],
update: [],
patch: [],
remove: []
remove: [],
},

error: {
Expand All @@ -32,6 +31,6 @@ export default {
create: [],
update: [],
patch: [],
remove: []
}
remove: [],
},
};
Loading

0 comments on commit 62c77ee

Please sign in to comment.