docta-package 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +36 -0
- package/src/config.ts +25 -0
- package/src/dto/input/doctor.ts +120 -0
- package/src/dto/input/education.ts +11 -0
- package/src/dto/input/faq.ts +11 -0
- package/src/dto/input/index.ts +9 -0
- package/src/dto/input/language.ts +13 -0
- package/src/dto/input/location.ts +31 -0
- package/src/dto/input/patient.ts +17 -0
- package/src/dto/input/position.ts +21 -0
- package/src/dto/input/specialty.ts +40 -0
- package/src/dto/input/user.ts +87 -0
- package/src/dto/output/doctor.ts +90 -0
- package/src/dto/output/education.ts +11 -0
- package/src/dto/output/faq.ts +11 -0
- package/src/dto/output/index.ts +9 -0
- package/src/dto/output/language.ts +11 -0
- package/src/dto/output/location.ts +21 -0
- package/src/dto/output/patient.ts +49 -0
- package/src/dto/output/position.ts +15 -0
- package/src/dto/output/specialty.ts +53 -0
- package/src/dto/output/user.ts +57 -0
- package/src/enums/gender.ts +4 -0
- package/src/enums/index.ts +4 -0
- package/src/enums/language-levels.ts +9 -0
- package/src/enums/status-codes.ts +28 -0
- package/src/enums/user-role.ts +5 -0
- package/src/errors/BadRequestError.ts +18 -0
- package/src/errors/CustomError.ts +18 -0
- package/src/errors/NotFoundError.ts +18 -0
- package/src/errors/UnAuthorizedError.ts +18 -0
- package/src/errors/index.ts +4 -0
- package/src/index.ts +19 -0
- package/src/interfaces/LoggedInUserToken.ts +9 -0
- package/src/interfaces/index.ts +1 -0
- package/src/middleware/errorHandler.ts +31 -0
- package/src/middleware/index.ts +5 -0
- package/src/middleware/multer.ts +74 -0
- package/src/middleware/require-auth.ts +46 -0
- package/src/middleware/validate-request.ts +40 -0
- package/src/middleware/verify-roles.ts +17 -0
- package/src/models/base.ts +52 -0
- package/src/models/doctor.ts +96 -0
- package/src/models/education.ts +14 -0
- package/src/models/faq.ts +14 -0
- package/src/models/index.ts +10 -0
- package/src/models/language.ts +20 -0
- package/src/models/location.ts +24 -0
- package/src/models/patient.ts +35 -0
- package/src/models/position.ts +19 -0
- package/src/models/specialty.ts +37 -0
- package/src/models/user.ts +67 -0
- package/src/utils/index.ts +5 -0
- package/src/utils/orchestration-result.ts +74 -0
- package/src/utils/s3-helper.ts +72 -0
- package/src/utils/token-utils.ts +86 -0
- package/src/utils/validate-info.ts +26 -0
- package/src/utils/winston.ts +33 -0
- package/tsconfig.json +120 -0
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export * from "./base";
|
|
2
|
+
export * from "./doctor";
|
|
3
|
+
export * from "./education";
|
|
4
|
+
export * from "./faq";
|
|
5
|
+
export * from "./language";
|
|
6
|
+
export * from "./location";
|
|
7
|
+
export * from "./patient";
|
|
8
|
+
export * from "./position";
|
|
9
|
+
export * from "./specialty";
|
|
10
|
+
export * from "./user";
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { Schema } from "mongoose";
|
|
2
|
+
import { EnumLanguageLevel } from "../enums";
|
|
3
|
+
|
|
4
|
+
export interface ILanguage {
|
|
5
|
+
title: string;
|
|
6
|
+
level: EnumLanguageLevel;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export const LanguageSchema = new Schema<ILanguage>(
|
|
10
|
+
{
|
|
11
|
+
title: { type: String, required: true, trim: true },
|
|
12
|
+
level: {
|
|
13
|
+
type: String,
|
|
14
|
+
enum: Object.values(EnumLanguageLevel),
|
|
15
|
+
required: true,
|
|
16
|
+
trim: true,
|
|
17
|
+
},
|
|
18
|
+
},
|
|
19
|
+
{ _id: false }
|
|
20
|
+
);
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { Schema } from "mongoose";
|
|
2
|
+
|
|
3
|
+
export interface ILocation {
|
|
4
|
+
address?: string;
|
|
5
|
+
city?: string;
|
|
6
|
+
country?: string; // ISO code eg: fr
|
|
7
|
+
lat?: number;
|
|
8
|
+
lng?: number;
|
|
9
|
+
zipcode?: string;
|
|
10
|
+
distanceInMeters?: number | null;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export const LocationSchema = new Schema<ILocation>(
|
|
14
|
+
{
|
|
15
|
+
address: { type: String, required: false, trim: true },
|
|
16
|
+
city: { type: String, required: false, trim: true },
|
|
17
|
+
country: { type: String, required: false, trim: true },
|
|
18
|
+
lat: { type: Number, required: false },
|
|
19
|
+
lng: { type: Number, required: false },
|
|
20
|
+
zipcode: { type: String, required: false, trim: true },
|
|
21
|
+
distanceInMeters: { type: Number, required: false, default: null },
|
|
22
|
+
},
|
|
23
|
+
{ _id: false }
|
|
24
|
+
);
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { Schema, model, Document, Model } from "mongoose";
|
|
2
|
+
import { IUserDocument, UserModel } from ".";
|
|
3
|
+
import { BaseSchemaFields, BaseSchemaPlugin, IBaseModel } from ".";
|
|
4
|
+
import { Gender } from "../enums";
|
|
5
|
+
|
|
6
|
+
export interface IPatient extends IBaseModel {
|
|
7
|
+
user: IUserDocument;
|
|
8
|
+
dob?: number;
|
|
9
|
+
gender?: Gender;
|
|
10
|
+
phoneNumber?: string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface IPatientDocument extends IPatient, Document {}
|
|
14
|
+
|
|
15
|
+
export interface IPatientModel extends Model<IPatientDocument> {}
|
|
16
|
+
|
|
17
|
+
const PatientSchema = new Schema<IPatientDocument>({
|
|
18
|
+
...BaseSchemaFields,
|
|
19
|
+
user: {
|
|
20
|
+
type: Schema.Types.ObjectId,
|
|
21
|
+
ref: UserModel.modelName,
|
|
22
|
+
required: true,
|
|
23
|
+
onDelete: "cascade",
|
|
24
|
+
},
|
|
25
|
+
dob: { type: Number, required: false },
|
|
26
|
+
phoneNumber: { type: String, required: false },
|
|
27
|
+
gender: { type: String, enum: Object.values(Gender), required: false },
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
PatientSchema.plugin(BaseSchemaPlugin);
|
|
31
|
+
|
|
32
|
+
export const PatientModel = model<IPatientDocument, IPatientModel>(
|
|
33
|
+
"Patient",
|
|
34
|
+
PatientSchema
|
|
35
|
+
);
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Schema } from "mongoose";
|
|
2
|
+
|
|
3
|
+
export interface IPosition {
|
|
4
|
+
startDate: number; // timestamp (ms)
|
|
5
|
+
endDate?: number; // timestamp (ms), optional
|
|
6
|
+
title: string;
|
|
7
|
+
company: string;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export const PositionSchema = new Schema<IPosition>(
|
|
11
|
+
{
|
|
12
|
+
startDate: { type: Number, required: true },
|
|
13
|
+
endDate: { type: Number, required: false },
|
|
14
|
+
title: { type: String, required: true, trim: true },
|
|
15
|
+
company: { type: String, required: true, trim: true },
|
|
16
|
+
},
|
|
17
|
+
{ _id: false }
|
|
18
|
+
);
|
|
19
|
+
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { Schema, model, Document, Model } from "mongoose";
|
|
2
|
+
import { BaseSchemaFields, BaseSchemaPlugin, IBaseModel } from ".";
|
|
3
|
+
|
|
4
|
+
export interface ILocalizedSpecialtyFields {
|
|
5
|
+
name: string;
|
|
6
|
+
description?: string | null;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export interface ISpecialty extends IBaseModel {
|
|
10
|
+
en: ILocalizedSpecialtyFields;
|
|
11
|
+
fr?: ILocalizedSpecialtyFields;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface ISpecialtyDocument extends ISpecialty, Document {}
|
|
15
|
+
|
|
16
|
+
export interface ISpecialtyModel extends Model<ISpecialtyDocument> {}
|
|
17
|
+
|
|
18
|
+
const LocalizedFieldsSchema = new Schema<ILocalizedSpecialtyFields>(
|
|
19
|
+
{
|
|
20
|
+
name: { type: String, required: true },
|
|
21
|
+
description: { type: String, default: null },
|
|
22
|
+
},
|
|
23
|
+
{ _id: false }
|
|
24
|
+
);
|
|
25
|
+
|
|
26
|
+
const SpecialtySchema = new Schema<ISpecialtyDocument>({
|
|
27
|
+
...BaseSchemaFields,
|
|
28
|
+
en: { type: LocalizedFieldsSchema, required: true },
|
|
29
|
+
fr: { type: LocalizedFieldsSchema, required: false, default: null },
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
SpecialtySchema.plugin(BaseSchemaPlugin);
|
|
33
|
+
|
|
34
|
+
export const SpecialtyModel = model<ISpecialtyDocument, ISpecialtyModel>(
|
|
35
|
+
"Specialty",
|
|
36
|
+
SpecialtySchema
|
|
37
|
+
);
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { Schema, model, Document, Model } from "mongoose";
|
|
2
|
+
import { EnumUserRole } from "../enums";
|
|
3
|
+
const bcrypt = require("bcryptjs");
|
|
4
|
+
import { IBaseModel, BaseSchemaFields, BaseSchemaPlugin } from ".";
|
|
5
|
+
|
|
6
|
+
export interface IUser extends IBaseModel {
|
|
7
|
+
role: EnumUserRole;
|
|
8
|
+
name: string;
|
|
9
|
+
email: string;
|
|
10
|
+
password?: string;
|
|
11
|
+
activationToken?: string | null;
|
|
12
|
+
forgotPasswordToken?: string | null;
|
|
13
|
+
token?: string | null;
|
|
14
|
+
isActive: boolean;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface IUserDocument extends IUser, Document {
|
|
18
|
+
comparePassword(candidatePassword: string): Promise<boolean>;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface IUserModel extends Model<IUserDocument> {
|
|
22
|
+
// toto(): void;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const UserSchema = new Schema<IUserDocument>({
|
|
26
|
+
...BaseSchemaFields,
|
|
27
|
+
name: { type: String, required: true },
|
|
28
|
+
email: { type: String, required: true, unique: true, trim: true },
|
|
29
|
+
password: { type: String, required: false, default: null },
|
|
30
|
+
activationToken: { type: String, default: null },
|
|
31
|
+
forgotPasswordToken: { type: String, default: null },
|
|
32
|
+
token: { type: String, default: null },
|
|
33
|
+
isActive: { type: Boolean, default: false },
|
|
34
|
+
role: {
|
|
35
|
+
type: String,
|
|
36
|
+
enum: Object.values(EnumUserRole),
|
|
37
|
+
default: EnumUserRole.PATIENT,
|
|
38
|
+
},
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
UserSchema.plugin(BaseSchemaPlugin);
|
|
42
|
+
|
|
43
|
+
UserSchema.pre<IUserDocument>("save", async function (next) {
|
|
44
|
+
if (!this.isModified("password")) {
|
|
45
|
+
return next();
|
|
46
|
+
}
|
|
47
|
+
try {
|
|
48
|
+
const salt = await bcrypt.genSalt(10);
|
|
49
|
+
this.password = await bcrypt.hash(this.password, salt);
|
|
50
|
+
return next();
|
|
51
|
+
} catch (error) {
|
|
52
|
+
return next(error as Error);
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
UserSchema.methods.comparePassword = async function (
|
|
57
|
+
candidatePassword: string
|
|
58
|
+
): Promise<boolean> {
|
|
59
|
+
return bcrypt.compare(candidatePassword, this.password);
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
export const UserModel = model<IUserDocument, IUserModel>("User", UserSchema);
|
|
63
|
+
|
|
64
|
+
// const tot = async (): Promise<IUserDocument[]> => {
|
|
65
|
+
// const user: IUserDocument[] = await UserModel.find({});
|
|
66
|
+
// return user;
|
|
67
|
+
// };
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { EnumStatusCode } from "../enums";
|
|
2
|
+
|
|
3
|
+
export type ErrorResult = {
|
|
4
|
+
code: EnumStatusCode;
|
|
5
|
+
message: string;
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
export type PaginatedResult<T> = {
|
|
9
|
+
code: EnumStatusCode;
|
|
10
|
+
message: string;
|
|
11
|
+
data: {
|
|
12
|
+
items: T[];
|
|
13
|
+
totalItems: number;
|
|
14
|
+
itemsPerPage: number;
|
|
15
|
+
page: number;
|
|
16
|
+
totalPages: number;
|
|
17
|
+
};
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export type SimpleItemResult<T> = {
|
|
21
|
+
code: EnumStatusCode;
|
|
22
|
+
message: string;
|
|
23
|
+
data:
|
|
24
|
+
| {
|
|
25
|
+
item: T;
|
|
26
|
+
}
|
|
27
|
+
| undefined;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export class OrchestrationResult {
|
|
31
|
+
static paginated<T>({
|
|
32
|
+
data,
|
|
33
|
+
totalItems,
|
|
34
|
+
itemsPerPage,
|
|
35
|
+
page,
|
|
36
|
+
code,
|
|
37
|
+
message,
|
|
38
|
+
}: {
|
|
39
|
+
data: T[];
|
|
40
|
+
totalItems: number;
|
|
41
|
+
itemsPerPage: number;
|
|
42
|
+
page: number;
|
|
43
|
+
code: EnumStatusCode;
|
|
44
|
+
message: string;
|
|
45
|
+
}): PaginatedResult<T> {
|
|
46
|
+
return {
|
|
47
|
+
code,
|
|
48
|
+
message,
|
|
49
|
+
data: {
|
|
50
|
+
items: data,
|
|
51
|
+
totalItems,
|
|
52
|
+
itemsPerPage,
|
|
53
|
+
page,
|
|
54
|
+
totalPages: Math.ceil(totalItems / itemsPerPage),
|
|
55
|
+
},
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
static item<T>({
|
|
60
|
+
code,
|
|
61
|
+
data,
|
|
62
|
+
message,
|
|
63
|
+
}: {
|
|
64
|
+
code: EnumStatusCode;
|
|
65
|
+
message: string;
|
|
66
|
+
data?: T;
|
|
67
|
+
}): SimpleItemResult<T> {
|
|
68
|
+
return {
|
|
69
|
+
code,
|
|
70
|
+
message,
|
|
71
|
+
data: data === undefined ? undefined : { item: data },
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DeleteObjectCommand,
|
|
3
|
+
GetObjectCommand,
|
|
4
|
+
PutObjectCommand,
|
|
5
|
+
S3Client,
|
|
6
|
+
} from "@aws-sdk/client-s3";
|
|
7
|
+
import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
|
|
8
|
+
import { generalConfig } from "../config";
|
|
9
|
+
|
|
10
|
+
export class AwsS3Helper {
|
|
11
|
+
private s3: S3Client;
|
|
12
|
+
private bucketName: string;
|
|
13
|
+
private bucketRegion: string;
|
|
14
|
+
private accessKey: string;
|
|
15
|
+
private secretKey: string;
|
|
16
|
+
private signedUrlExpiry = 3600; // 1 hour
|
|
17
|
+
|
|
18
|
+
constructor() {
|
|
19
|
+
this.bucketName = generalConfig.awsS3Bucket;
|
|
20
|
+
this.bucketRegion = generalConfig.awsS3Region;
|
|
21
|
+
this.accessKey = generalConfig.awsAccessKey;
|
|
22
|
+
this.secretKey = generalConfig.awsSecretKey;
|
|
23
|
+
|
|
24
|
+
this.s3 = new S3Client({
|
|
25
|
+
credentials: {
|
|
26
|
+
accessKeyId: this.accessKey,
|
|
27
|
+
secretAccessKey: this.secretKey,
|
|
28
|
+
},
|
|
29
|
+
region: this.bucketRegion,
|
|
30
|
+
requestHandler: { socketTimeout: 120000 },
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
private async sendCommand(command: any) {
|
|
35
|
+
return this.s3.send(command);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
async uploadImage(
|
|
39
|
+
key: string,
|
|
40
|
+
contentType: string,
|
|
41
|
+
file: Buffer
|
|
42
|
+
): Promise<string> {
|
|
43
|
+
const s3Key = key;
|
|
44
|
+
|
|
45
|
+
await this.sendCommand(
|
|
46
|
+
new PutObjectCommand({
|
|
47
|
+
Bucket: this.bucketName,
|
|
48
|
+
Key: s3Key,
|
|
49
|
+
Body: file,
|
|
50
|
+
ContentType: contentType,
|
|
51
|
+
})
|
|
52
|
+
);
|
|
53
|
+
return s3Key;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
async getImageUrl(key: string): Promise<string> {
|
|
57
|
+
const command = new GetObjectCommand({
|
|
58
|
+
Bucket: this.bucketName,
|
|
59
|
+
Key: key,
|
|
60
|
+
});
|
|
61
|
+
return getSignedUrl(this.s3, command, { expiresIn: this.signedUrlExpiry });
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
async deleteImageFromS3(key: string) {
|
|
65
|
+
await this.sendCommand(
|
|
66
|
+
new DeleteObjectCommand({
|
|
67
|
+
Bucket: this.bucketName,
|
|
68
|
+
Key: key,
|
|
69
|
+
})
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import jwt, { JwtPayload } from "jsonwebtoken";
|
|
2
|
+
import { LoggedInUserTokenData } from "../interfaces";
|
|
3
|
+
import { generalConfig } from "../config";
|
|
4
|
+
|
|
5
|
+
export class TokenUtils {
|
|
6
|
+
static createActivationToken(userId: string): string {
|
|
7
|
+
return jwt.sign({ userId }, generalConfig.accessTokenSecret);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
static decodeActivationToken(token: string): string | null {
|
|
11
|
+
try {
|
|
12
|
+
const decoded = jwt.verify(token, generalConfig.activationTokenSecret) as
|
|
13
|
+
| JwtPayload
|
|
14
|
+
| string;
|
|
15
|
+
if (typeof decoded === "string") {
|
|
16
|
+
return null;
|
|
17
|
+
}
|
|
18
|
+
return typeof decoded.userId === "string" ? decoded.userId : null;
|
|
19
|
+
} catch {
|
|
20
|
+
return null;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
static createAccessToken(payload: LoggedInUserTokenData): string {
|
|
25
|
+
return jwt.sign(payload, generalConfig.accessTokenSecret, {
|
|
26
|
+
expiresIn: generalConfig.accessTokenExpiry,
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
static createRefreshToken(payload: LoggedInUserTokenData): string {
|
|
31
|
+
return jwt.sign(payload, generalConfig.refreshTokenSecret, {
|
|
32
|
+
expiresIn: generalConfig.refreshTokenExpiry,
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
static verifyRefreshToken(token: string): LoggedInUserTokenData | null {
|
|
37
|
+
try {
|
|
38
|
+
const decoded = jwt.verify(
|
|
39
|
+
token,
|
|
40
|
+
generalConfig.refreshTokenSecret
|
|
41
|
+
) as JwtPayload;
|
|
42
|
+
return {
|
|
43
|
+
id: decoded.id,
|
|
44
|
+
email: decoded.email,
|
|
45
|
+
role: decoded.role,
|
|
46
|
+
};
|
|
47
|
+
} catch {
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
static verifyAccessToken(token: string): LoggedInUserTokenData {
|
|
53
|
+
try {
|
|
54
|
+
const decoded = jwt.verify(
|
|
55
|
+
token,
|
|
56
|
+
generalConfig.accessTokenSecret
|
|
57
|
+
) as JwtPayload;
|
|
58
|
+
return {
|
|
59
|
+
id: decoded.id,
|
|
60
|
+
email: decoded.email,
|
|
61
|
+
role: decoded.role,
|
|
62
|
+
};
|
|
63
|
+
} catch (error) {
|
|
64
|
+
throw error;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
static createForgotPasswordToken(userId: string): string {
|
|
69
|
+
return jwt.sign({ userId }, generalConfig.forgotPasswordTokenSecret);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
static decodeForgotPasswordToken(token: string): string | null {
|
|
73
|
+
try {
|
|
74
|
+
const decoded = jwt.verify(
|
|
75
|
+
token,
|
|
76
|
+
generalConfig.forgotPasswordTokenSecret
|
|
77
|
+
) as JwtPayload | string;
|
|
78
|
+
if (typeof decoded === "string") {
|
|
79
|
+
return null;
|
|
80
|
+
}
|
|
81
|
+
return typeof decoded.userId === "string" ? decoded.userId : null;
|
|
82
|
+
} catch {
|
|
83
|
+
return null;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { EnumStatusCode } from "../enums";
|
|
2
|
+
import { NotFoundError } from "../errors";
|
|
3
|
+
import { UnAuthorizedError } from "../errors";
|
|
4
|
+
import { IUserDocument } from "../models";
|
|
5
|
+
|
|
6
|
+
export class ValidateInfo {
|
|
7
|
+
static validateUser(user: IUserDocument): void {
|
|
8
|
+
if (!user) {
|
|
9
|
+
throw new NotFoundError(EnumStatusCode.NOT_FOUND, "User not found");
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
if (user.isDeleted) {
|
|
13
|
+
throw new UnAuthorizedError(
|
|
14
|
+
EnumStatusCode.ACCOUNT_DELETED,
|
|
15
|
+
"Your account has been deleted"
|
|
16
|
+
);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
if (!user.isActive) {
|
|
20
|
+
throw new UnAuthorizedError(
|
|
21
|
+
EnumStatusCode.ACCOUNT_DEACTIVATED,
|
|
22
|
+
"Account is deactivated"
|
|
23
|
+
);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
// src/utils/logger.ts
|
|
2
|
+
import winston from "winston";
|
|
3
|
+
import "winston-daily-rotate-file";
|
|
4
|
+
import path from "path";
|
|
5
|
+
import fs from "fs";
|
|
6
|
+
|
|
7
|
+
const logDir = path.join(process.cwd(), "logs");
|
|
8
|
+
|
|
9
|
+
// ensure logs directory exists
|
|
10
|
+
if (!fs.existsSync(logDir)) {
|
|
11
|
+
fs.mkdirSync(logDir, { recursive: true });
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const transport = new winston.transports.DailyRotateFile({
|
|
15
|
+
dirname: logDir,
|
|
16
|
+
filename: "%DATE%.log",
|
|
17
|
+
datePattern: "YYYY-MM-DD",
|
|
18
|
+
zippedArchive: false,
|
|
19
|
+
maxSize: "20m",
|
|
20
|
+
maxFiles: "14d",
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
export const logger = winston.createLogger({
|
|
24
|
+
level: "error",
|
|
25
|
+
format: winston.format.combine(
|
|
26
|
+
winston.format.timestamp(),
|
|
27
|
+
winston.format.json()
|
|
28
|
+
),
|
|
29
|
+
transports: [
|
|
30
|
+
transport,
|
|
31
|
+
new winston.transports.Console({ format: winston.format.simple() }), // optional
|
|
32
|
+
],
|
|
33
|
+
});
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
/* Visit https://aka.ms/tsconfig to read more about this file */
|
|
4
|
+
"rootDir": "./src" /* Specify the root folder within your source files. */,
|
|
5
|
+
|
|
6
|
+
/* Projects */
|
|
7
|
+
// "incremental": true, /* Save .tsbuildinfo files to allow for incremental compilation of projects. */
|
|
8
|
+
// "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */
|
|
9
|
+
// "tsBuildInfoFile": "./.tsbuildinfo", /* Specify the path to .tsbuildinfo incremental compilation file. */
|
|
10
|
+
// "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects. */
|
|
11
|
+
// "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */
|
|
12
|
+
// "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */
|
|
13
|
+
|
|
14
|
+
/* Language and Environment */
|
|
15
|
+
"target": "es2016" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */,
|
|
16
|
+
// "lib": [],
|
|
17
|
+
"lib": ["es2017"],
|
|
18
|
+
/* Specify a set of bundled library declaration files that describe the target runtime environment. */
|
|
19
|
+
// "jsx": "preserve", /* Specify what JSX code is generated. */
|
|
20
|
+
// "libReplacement": true, /* Enable lib replacement. */
|
|
21
|
+
"experimentalDecorators": true /* Enable experimental support for legacy experimental decorators. */,
|
|
22
|
+
"emitDecoratorMetadata": true /* Emit design-type metadata for decorated declarations in source files. */,
|
|
23
|
+
// "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h'. */
|
|
24
|
+
// "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */
|
|
25
|
+
// "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using 'jsx: react-jsx*'. */
|
|
26
|
+
// "reactNamespace": "", /* Specify the object invoked for 'createElement'. This only applies when targeting 'react' JSX emit. */
|
|
27
|
+
// "noLib": true, /* Disable including any library files, including the default lib.d.ts. */
|
|
28
|
+
// "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */
|
|
29
|
+
// "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */
|
|
30
|
+
|
|
31
|
+
/* Modules */
|
|
32
|
+
"module": "commonjs" /* Specify what module code is generated. */,
|
|
33
|
+
// "rootDir": "./", /* Specify the root folder within your source files. */
|
|
34
|
+
// "moduleResolution": "node10", /* Specify how TypeScript looks up a file from a given module specifier. */
|
|
35
|
+
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
|
|
36
|
+
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
|
|
37
|
+
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
|
|
38
|
+
// "typeRoots": [], /* Specify multiple folders that act like './node_modules/@types'. */
|
|
39
|
+
"types": [
|
|
40
|
+
"node",
|
|
41
|
+
"jest"
|
|
42
|
+
] /* Specify type package names to be included without being referenced in a source file. */,
|
|
43
|
+
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
|
|
44
|
+
// "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */
|
|
45
|
+
// "allowImportingTsExtensions": true, /* Allow imports to include TypeScript file extensions. Requires '--moduleResolution bundler' and either '--noEmit' or '--emitDeclarationOnly' to be set. */
|
|
46
|
+
// "rewriteRelativeImportExtensions": true, /* Rewrite '.ts', '.tsx', '.mts', and '.cts' file extensions in relative import paths to their JavaScript equivalent in output files. */
|
|
47
|
+
// "resolvePackageJsonExports": true, /* Use the package.json 'exports' field when resolving package imports. */
|
|
48
|
+
// "resolvePackageJsonImports": true, /* Use the package.json 'imports' field when resolving imports. */
|
|
49
|
+
// "customConditions": [], /* Conditions to set in addition to the resolver-specific defaults when resolving imports. */
|
|
50
|
+
// "noUncheckedSideEffectImports": true, /* Check side effect imports. */
|
|
51
|
+
// "resolveJsonModule": true, /* Enable importing .json files. */
|
|
52
|
+
// "allowArbitraryExtensions": true, /* Enable importing files with any extension, provided a declaration file is present. */
|
|
53
|
+
// "noResolve": true, /* Disallow 'import's, 'require's or '<reference>'s from expanding the number of files TypeScript should add to a project. */
|
|
54
|
+
|
|
55
|
+
/* JavaScript Support */
|
|
56
|
+
// "allowJs": true, /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */
|
|
57
|
+
// "checkJs": true, /* Enable error reporting in type-checked JavaScript files. */
|
|
58
|
+
// "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */
|
|
59
|
+
|
|
60
|
+
/* Emit */
|
|
61
|
+
// "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
|
|
62
|
+
// "declarationMap": true, /* Create sourcemaps for d.ts files. */
|
|
63
|
+
// "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */
|
|
64
|
+
// "sourceMap": true, /* Create source map files for emitted JavaScript files. */
|
|
65
|
+
// "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */
|
|
66
|
+
// "noEmit": true, /* Disable emitting files from a compilation. */
|
|
67
|
+
// "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */
|
|
68
|
+
"outDir": "./build" /* Specify an output folder for all emitted files. */,
|
|
69
|
+
// "removeComments": true, /* Disable emitting comments. */
|
|
70
|
+
// "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
|
|
71
|
+
// "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */
|
|
72
|
+
// "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */
|
|
73
|
+
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
|
|
74
|
+
// "inlineSources": true, /* Include source code in the sourcemaps inside the emitted JavaScript. */
|
|
75
|
+
// "emitBOM": true, /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */
|
|
76
|
+
// "newLine": "crlf", /* Set the newline character for emitting files. */
|
|
77
|
+
// "stripInternal": true, /* Disable emitting declarations that have '@internal' in their JSDoc comments. */
|
|
78
|
+
// "noEmitHelpers": true, /* Disable generating custom helper functions like '__extends' in compiled output. */
|
|
79
|
+
"noEmitOnError": true /* Disable emitting files if any type checking errors are reported. */,
|
|
80
|
+
// "preserveConstEnums": true, /* Disable erasing 'const enum' declarations in generated code. */
|
|
81
|
+
// "declarationDir": "./", /* Specify the output directory for generated declaration files. */
|
|
82
|
+
|
|
83
|
+
/* Interop Constraints */
|
|
84
|
+
// "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */
|
|
85
|
+
// "verbatimModuleSyntax": true, /* Do not transform or elide any imports or exports not marked as type-only, ensuring they are written in the output file's format based on the 'module' setting. */
|
|
86
|
+
// "isolatedDeclarations": true, /* Require sufficient annotation on exports so other tools can trivially generate declaration files. */
|
|
87
|
+
// "erasableSyntaxOnly": true, /* Do not allow runtime constructs that are not part of ECMAScript. */
|
|
88
|
+
// "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
|
|
89
|
+
"esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */,
|
|
90
|
+
// "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
|
|
91
|
+
"forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */,
|
|
92
|
+
|
|
93
|
+
/* Type Checking */
|
|
94
|
+
"strict": true /* Enable all strict type-checking options. */,
|
|
95
|
+
// "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied 'any' type. */
|
|
96
|
+
// "strictNullChecks": true, /* When type checking, take into account 'null' and 'undefined'. */
|
|
97
|
+
// "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */
|
|
98
|
+
// "strictBindCallApply": true, /* Check that the arguments for 'bind', 'call', and 'apply' methods match the original function. */
|
|
99
|
+
"strictPropertyInitialization": false /* Check for class properties that are declared but not set in the constructor. */,
|
|
100
|
+
// "strictBuiltinIteratorReturn": true, /* Built-in iterators are instantiated with a 'TReturn' type of 'undefined' instead of 'any'. */
|
|
101
|
+
// "noImplicitThis": true, /* Enable error reporting when 'this' is given the type 'any'. */
|
|
102
|
+
// "useUnknownInCatchVariables": true, /* Default catch clause variables as 'unknown' instead of 'any'. */
|
|
103
|
+
// "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */
|
|
104
|
+
// "noUnusedLocals": true, /* Enable error reporting when local variables aren't read. */
|
|
105
|
+
// "noUnusedParameters": true, /* Raise an error when a function parameter isn't read. */
|
|
106
|
+
// "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */
|
|
107
|
+
// "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */
|
|
108
|
+
// "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */
|
|
109
|
+
// "noUncheckedIndexedAccess": true, /* Add 'undefined' to a type when accessed using an index. */
|
|
110
|
+
// "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */
|
|
111
|
+
// "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type. */
|
|
112
|
+
// "allowUnusedLabels": true, /* Disable error reporting for unused labels. */
|
|
113
|
+
// "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */
|
|
114
|
+
|
|
115
|
+
/* Completeness */
|
|
116
|
+
// "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
|
|
117
|
+
"skipLibCheck": true /* Skip type checking all .d.ts files. */
|
|
118
|
+
},
|
|
119
|
+
"include": ["src"]
|
|
120
|
+
}
|