Skip to content

Commit

Permalink
style: fix linting errors
Browse files Browse the repository at this point in the history
  • Loading branch information
davidzlu committed Oct 2, 2024
1 parent 8382829 commit 6007601
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 36 deletions.
4 changes: 2 additions & 2 deletions backend/src/models/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export interface ConfigType {
}

interface ConfigStatics extends Model<ConfigType> {
joi(obj: any, options: object): Joi.ValidationResult<any>,
joi(obj: unknown, options?: object): Joi.ValidationResult<unknown>,
}

const configSchema = new Schema<ConfigType, ConfigStatics>({
Expand All @@ -34,7 +34,7 @@ const modelFieldValidation = Joi.string()
'string.pattern.base': 'Field must only contain alphanumeric characters, hyphens, and periods.'
});

configSchema.statics.joi = function (obj: any, options: object): Joi.ValidationResult<any> {
configSchema.statics.joi = function (obj: unknown, options?: object): Joi.ValidationResult<unknown> {
const joiConfigSchema = Joi.object({
model: Joi.object({
chat: modelFieldValidation,
Expand Down
6 changes: 3 additions & 3 deletions backend/src/models/entry/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ export interface EntryType {
updated_at?: Date,
privacy_settings?: {
public?: boolean,
shared_with?: Types.ObjectId,
shared_with?: Types.ObjectId[],
},
analysis?: Types.ObjectId,
conversation?: Types.ObjectId,
}

interface EntryStatics extends Model<EntryType> {
joi(obj: any, options: object): Joi.ValidationResult<any>,
joi(obj: unknown, options?: object): Joi.ValidationResult,
}

const entrySchema = new Schema<EntryType, EntryStatics>({
Expand All @@ -37,7 +37,7 @@ const entrySchema = new Schema<EntryType, EntryStatics>({
conversation: { type: Schema.Types.ObjectId, ref: 'EntryConversation' }
});

entrySchema.statics.joi = function (obj: any, options: object): Joi.ValidationResult<any> {
entrySchema.statics.joi = function (obj: unknown, options?: object): Joi.ValidationResult {
const joiEntrySchema = Joi.object({
title: Joi.string()
.allow('')
Expand Down
12 changes: 10 additions & 2 deletions backend/src/models/entry/entryAnalysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,19 @@ export interface EntryAnalysisType {
updated_at?: Date,
}

/* eslint-disable @typescript-eslint/no-explicit-any */
// TODO: any should be replaced with return type from cdgpt response
interface EntryAnalysisMethods {
getAnalysisContent(configId: string, content: string): Promise<any>,
}
/* eslint-enable @typescript-eslint/no-explicit-any */

/* eslint-disable @typescript-eslint/no-empty-object-type */
// Done to match: https://mongoosejs.com/docs/typescript/statics-and-methods.html
interface EntryAnalysisStatics extends Model<EntryAnalysisType, {}, EntryAnalysisMethods> {
joi(obj: any, options: object): Joi.ValidationResult<any>,
joi(obj: unknown, options?: object): Joi.ValidationResult,
}
/* eslint-enable @typescript-eslint/no-empty-object-type */

const entryAnalysisSchema = new Schema<EntryAnalysisType, EntryAnalysisStatics, EntryAnalysisMethods>({
entry: { type: Schema.Types.ObjectId, ref: 'Entry', required: true },
Expand All @@ -28,7 +34,7 @@ const entryAnalysisSchema = new Schema<EntryAnalysisType, EntryAnalysisStatics,
updated_at: { type: Date, default: Date.now }
});

entryAnalysisSchema.statics.joi = function (obj: any, options: object): Joi.ValidationResult<any> {
entryAnalysisSchema.statics.joi = function (obj: unknown, options?: object): Joi.ValidationResult {
const entryAnalysisJoiSchema = Joi.object({
analysis_content: Joi.string()
.allow('')
Expand All @@ -48,6 +54,7 @@ entryAnalysisSchema.pre('save', function (next) {
next();
});

/* eslint-disable @typescript-eslint/no-explicit-any */
//TODO: pull out this method to somewhere else. dependency on CdGpt not great
// Get the analysis content for an entry
entryAnalysisSchema.methods.getAnalysisContent = async function (configId: string, content: string): Promise<any> {
Expand Down Expand Up @@ -94,4 +101,5 @@ entryAnalysisSchema.methods.getAnalysisContent = async function (configId: strin
return response;
};

/* eslint-enable @typescript-eslint/no-explicit-any */
export default model<EntryAnalysisType, EntryAnalysisStatics>('EntryAnalysis', entryAnalysisSchema);
10 changes: 7 additions & 3 deletions backend/src/models/entry/entryConversation.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable sort-imports */
import { Config, EntryAnalysis } from '../index.js';
import { Model, Schema, Types, model } from 'mongoose';
import { Model, model, Schema, Types } from 'mongoose';

import CdGpt, { ChatMessage } from '../../assistants/gpts/CdGpt.js';

Expand All @@ -19,9 +20,12 @@ interface EntryConversationMethods {
getChatContent(configId: string, analysisId: string, content: string, messages: []): Promise<string>,
}

/* eslint-disable @typescript-eslint/no-empty-object-type */
// Done to match: https://mongoosejs.com/docs/typescript/statics-and-methods.html
interface EntryConversationStatics extends Model<EntryConversationType, {}, EntryConversationMethods> {
joi(obj: any, options: object): Joi.ValidationResult<any>,
joi(obj: unknown, options?: object): Joi.ValidationResult,
}
/* eslint-enable @typescript-eslint/no-empty-object-type */

const entryConversationSchema = new Schema<EntryConversationType, EntryConversationStatics, EntryConversationMethods>({
entry: { type: Schema.Types.ObjectId, ref: 'Entry', required: true },
Expand All @@ -32,7 +36,7 @@ const entryConversationSchema = new Schema<EntryConversationType, EntryConversat
}]
});

entryConversationSchema.statics.joi = function (obj: any, options: object): Joi.ValidationResult<any> {
entryConversationSchema.statics.joi = function (obj: unknown, options?: object): Joi.ValidationResult {
const entryConversationJoiSchema = Joi.object({
messages: Joi.array().items(Joi.object({
message_content: Joi.string()
Expand Down
4 changes: 2 additions & 2 deletions backend/src/models/journal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export interface JournalType {
}

interface JournalStatics extends Model<JournalType> {
joi(obj: any, options: object): Joi.ValidationResult<any>,
joi(obj: unknown, options?: object): Joi.ValidationResult,
}

const journalSchema = new Schema<JournalType, JournalStatics>({
Expand All @@ -21,7 +21,7 @@ const journalSchema = new Schema<JournalType, JournalStatics>({
updated_at: { type: Date, default: Date.now }
});

journalSchema.statics.joi = function (obj: any, options: object): Joi.ValidationResult<any> {
journalSchema.statics.joi = function (obj: unknown, options?: object): Joi.ValidationResult {
const journalJoiSchema = Joi.object({
title: Joi.string()
.max(100)
Expand Down
40 changes: 23 additions & 17 deletions backend/src/models/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ export interface UserType extends PassportLocalDocument {
betaAccess?: boolean,

// Instance Methods
comparePassword(candidatePassword: string): Promise<any>,
checkEmail(email: string): Promise<any>,
comparePassword(candidatePassword: string): Promise<unknown>,
checkEmail(email: string): Promise<unknown>,
generatePasswordResetToken(): string,
generateEmailVerificationToken(): string,
generateBetaAccessToken(): string,
sendMail(content: any): Promise<void>,
sendMail(content: MailContent): Promise<void>,
sendPasswordResetEmail(token: string): Promise<void>,
sendPasswordResetConfirmationEmail(): Promise<void>,
sendBetaAccessVerificationEmail(token: string): Promise<void>,
Expand All @@ -47,11 +47,17 @@ export interface UserType extends PassportLocalDocument {
sendAlertForForgotPasswordAbuse(token: string): Promise<void>,
}

interface MailContent {
to: string,
subject: string,
text: string,
}

interface UserModel<T extends Document> extends PassportLocalModel<T> {
baseJoi(obj: any, options: object): Joi.ValidationResult<any>,
registrationJoi(obj: any, options: object): Joi.ValidationResult<any>,
passwordJoi(obj: any, options: object): Joi.ValidationResult<any>,
accountJoi(obj: any, options: object): Joi.ValidationResult<any>,
baseJoi(obj: unknown, options?: object): Joi.ValidationResult,
registrationJoi(obj: unknown, options?: object): Joi.ValidationResult,
passwordJoi(obj: unknown, options?: object): Joi.ValidationResult,
accountJoi(obj: unknown, options?: object): Joi.ValidationResult,
}

const userSchema = new Schema<UserType, UserModel<UserType>>({
Expand Down Expand Up @@ -154,12 +160,12 @@ const baseUserJoiSchema = Joi.object({

// Validation schemas
// Base Joi validation schema
userSchema.statics.baseJoi = function (obj: any, options: object): Joi.ValidationResult<any> {
userSchema.statics.baseJoi = function (obj: unknown, options?: object): Joi.ValidationResult {
return baseUserJoiSchema.validate(obj, options);
};

// Registration Joi validation schema
userSchema.statics.registrationJoi = function (obj: any, options: object): Joi.ValidationResult<any> {
userSchema.statics.registrationJoi = function (obj: unknown, options?: object): Joi.ValidationResult {
const registrationJoiSchema = baseUserJoiSchema.keys({
fname: createNameValidation(true),
lname: createNameValidation(true),
Expand All @@ -168,15 +174,15 @@ userSchema.statics.registrationJoi = function (obj: any, options: object): Joi.V
};

// New password Joi validation schema
userSchema.statics.passwordJoi = function (obj: any, options: object): Joi.ValidationResult<any> {
userSchema.statics.passwordJoi = function (obj: unknown, options?: object): Joi.ValidationResult {
const passwordJoiSchema = Joi.object({
newPassword: createPasswordValidation(true),
});
return passwordJoiSchema.validate(obj, options);
};

// Account Joi validation schema (fields are not required here)
userSchema.statics.accountJoi = function (obj: any, options: object): Joi.ValidationResult<any> {
userSchema.statics.accountJoi = function (obj: unknown, options?: object): Joi.ValidationResult {
const accountJoiSchema = Joi.object({
fname: createNameValidation(),
lname: createNameValidation(),
Expand Down Expand Up @@ -207,9 +213,9 @@ userSchema.pre('save', function (next) {

// User schema methods and statics
// Compare passwords for re-authentication
userSchema.methods.comparePassword = function (candidatePassword: string): Promise<any> {
userSchema.methods.comparePassword = function (candidatePassword: string): Promise<unknown> {
return new Promise((resolve, reject) => {
this.authenticate(candidatePassword, (err: any, user: any) => {
this.authenticate(candidatePassword, (err: unknown, user: unknown) => {
if (err) return reject(err);
if (user) return resolve(true);
return resolve(false);
Expand All @@ -218,9 +224,9 @@ userSchema.methods.comparePassword = function (candidatePassword: string): Promi
};

// Check if there is a user with the given email
userSchema.statics.checkEmail = function (email: string): Promise<any> {
userSchema.statics.checkEmail = function (email: string): Promise<unknown> {
return new Promise((resolve, reject) => {
this.findByUsername(email, false, (err: any, user: any) => {
this.findByUsername(email, false, (err: unknown, user: unknown) => {
if (err) return reject(err);
if (user) return resolve(true);
return resolve(false);
Expand Down Expand Up @@ -280,7 +286,7 @@ userSchema.methods.generateBetaAccessToken = function (): string {
};

// Utility function for sending emails
userSchema.methods.sendMail = async function (content: any): Promise<void> {
userSchema.methods.sendMail = async function (content: MailContent): Promise<void> {
const { to, subject, text } = content;

// Configure your SMTP transporter
Expand Down Expand Up @@ -377,7 +383,7 @@ userSchema.methods.sendBetaDenialEmail = async function (): Promise<void> {
const to = this.email;
const subject = 'Beta Access Denied';
const text = `Dear ${this.fname
},\n\nAfter reviewing your request for beta access, we have decided to deny your request. There may be a number of reasons why we made this decision such as the beta period ending soon or we have reached our maximum number of beta users. Whatever the case, you may apply again after ${this.betaAccessTokenExpires.toLocaleDateString()}. Thank you for your interest in the app! We hope you will consider applying again after the specified date or using the app when it is released.\n\nSincerely,\n\nThe CDJ Team\n`;
},\n\nAfter reviewing your request for beta access, we have decided to deny your request. There may be a number of reasons why we made this decision such as the beta period ending soon or we have reached our maximum number of beta users. Whatever the case, you may apply again after ${this.betaAccessTokenExpires.toLocaleDateString()}. Thank you for your interest in the app! We hope you will consider applying again after the specified date or using the app when it is released.\n\nSincerely,\n\nThe CDJ Team\n`;

this.sendMail({ to, subject, text });
};
Expand Down
18 changes: 14 additions & 4 deletions backend/tests/models/entry/entry.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/**
* @jest-environment node
*/
/* eslint-disable @typescript-eslint/no-explicit-any */

import { Entry } from "../../../src/models/index.js";
import { Entry } from '../../../src/models/index.js';

describe('Entry model tests', () => {
let entryObject: any;
Expand All @@ -16,7 +17,7 @@ describe('Entry model tests', () => {
tags: ['test', 'mock', 'jest'],
privacy_settings: {
public: false,
shared_with: ['friend1', 'therapist1']
shared_with: ['friend1id', 'therapist1id']
},
};
expectedResult = {
Expand All @@ -26,7 +27,7 @@ describe('Entry model tests', () => {
tags: ['test', 'mock', 'jest'],
privacy_settings: {
public: false,
shared_with: ['friend1', 'therapist1']
shared_with: ['friend1id', 'therapist1id']
},
};
});
Expand All @@ -40,7 +41,7 @@ describe('Entry model tests', () => {

it('replaces undefined title with Untitled', () => {
delete entryObject.title;
expectedResult.title = 'Untitled'
expectedResult.title = 'Untitled';

const { error, value } = Entry.joi(entryObject, {});

Expand Down Expand Up @@ -178,4 +179,13 @@ describe('Entry model tests', () => {
expect(value).toStrictEqual(expectedResult);
});

it('returns error if property not in schema passed in', () => {
entryObject.invalid_property = 'not in schema';
expectedResult.invalid_property = 'not in schema';

const { error, value } = Entry.joi(entryObject);

expect(error).toBeDefined();
expect(value).toStrictEqual(expectedResult);
});
});
6 changes: 3 additions & 3 deletions backend/tests/models/user.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,9 @@ describe('User model and validation tests', () => {
});

it.skip('sends an email with nodemailer', async () => {
// skipping test because kinda pointless and brittle,
// but it's a nice example if you want to test other email methods
const sendMailFn = jest.fn()
// Skipping test because kinda pointless and brittle,
// But it's a nice example if you want to test other email methods
const sendMailFn = jest.fn();
mockedNodemailer.createTransport.mockReturnValue(({ sendMail: sendMailFn } as unknown) as Transporter);
const content = {
to: '',
Expand Down

0 comments on commit 6007601

Please sign in to comment.