Skip to content

Commit

Permalink
chore : update files to js & npm test
Browse files Browse the repository at this point in the history
  • Loading branch information
Mansourkira committed Mar 17, 2024
1 parent 06086b2 commit 9b4ed31
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 86 deletions.
10 changes: 2 additions & 8 deletions test/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
const Hajar = require("../src/index").default;
const { hajar } = require("../www/src/@sikka/hajar/index");

// Replace with actual tests
describe("Hajar.src.js", () => {
it("should get the library's version", () => {
expect(Hajar.version).toEqual("1.0.41");
});

it("should get the library's name", () => {
const name = Hajar._name;
expect(name).toEqual("Hajar");
expect(hajar.version).toEqual("1.0.41");
});
});
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
"forceConsistentCasingInFileNames": true // Disallow inconsistently-cased references to the same file
},
"include": [
"www/src/@sikka/hajar/core/**/*", // Include all files in the src directory
"www/src/@sikka/hajar/index.ts"
"www/src/@sikka/hajar/core/**/*",
"www/src/@sikka/hajar/index.js"
],
"exclude": [
"node_modules", // Exclude the node_modules directory
Expand Down
6 changes: 6 additions & 0 deletions www/src/@sikka/hajar/core/auth/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const login = require("./login");

// @TODO add register function here
module.exports = {
login,
};
1 change: 0 additions & 1 deletion www/src/@sikka/hajar/core/auth/index.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,19 +1,8 @@
import jwt from "jsonwebtoken";
import bcrypt from "bcrypt";
import { secret } from "../init";
import { User } from "../init";
const jwt = require("jsonwebtoken");
const bcrypt = require("bcrypt");
const { User, secret } = require("../init.js");

interface ILoginResponse {
success: boolean;
user: object;
token: string;
}

async function login(
userType: string,
email: string,
password: string
): Promise<ILoginResponse> {
async function login(userType, email, password) {
const user = await User.findOne({ email, ref: userType });

if (!user) {
Expand All @@ -33,4 +22,4 @@ async function login(
};
}

export default login;
module.exports = login;
Original file line number Diff line number Diff line change
@@ -1,30 +1,18 @@
import mongoose, { Model } from "mongoose";

interface IUser {
findOne: (query: any) => Promise<any>;
}

interface IAdmin {
findOne: (query: any) => Promise<any>;
}

interface IClient {
findOne: (query: any) => Promise<any>;
}
const mongoose = require("mongoose");

let initialized = false;
let secret: string;
let mongooseInstance: typeof mongoose;
let User: IUser;
let Admin: IAdmin;
let Client: IClient;
let secret;
let mongooseInstance;
let User;
let Admin;
let Client;

export function initHajar(
jwtSecret: string,
inputMongooseInstance: typeof mongoose,
userModel: IUser,
adminModel: IAdmin,
clientModel: IClient
function initHajar(
jwtSecret,
inputMongooseInstance,
userModel,
adminModel,
clientModel
) {
if (initialized) {
throw new Error("Hajar is already initialized");
Expand All @@ -38,15 +26,15 @@ export function initHajar(
initialized = true;
}

export async function getUserType(email: string): Promise<string> {
async function getUserType(email) {
const user = await User.findOne({ email });
if (!user) {
throw new Error("User not found");
}
return user.ref;
}

export async function getAdminData(user: any): Promise<any> {
async function getAdminData(user) {
if (user.ref === "admin") {
const adminData = await Admin.findOne({ uid: user._id });
if (!adminData) {
Expand All @@ -57,7 +45,7 @@ export async function getAdminData(user: any): Promise<any> {
return null;
}

export async function getClientData(user: any): Promise<any> {
async function getClientData(user) {
if (user.ref === "client") {
const clientData = await Client.findOne({ uid: user._id });
if (!clientData) {
Expand All @@ -68,4 +56,14 @@ export async function getClientData(user: any): Promise<any> {
return null;
}

export { secret, mongooseInstance, User, Admin, Client };
module.exports = {
initHajar,
getUserType,
getAdminData,
getClientData,
secret,
mongooseInstance,
User,
Admin,
Client,
};
11 changes: 11 additions & 0 deletions www/src/@sikka/hajar/core/utils/HajarError.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class HajarError extends Error {
constructor(message, slug, customProperties) {
super(message);
this.slug = slug;
if (customProperties) {
Object.assign(this, customProperties);
}
}
}

module.exports = HajarError;
22 changes: 0 additions & 22 deletions www/src/@sikka/hajar/core/utils/HajarError.ts

This file was deleted.

11 changes: 11 additions & 0 deletions www/src/@sikka/hajar/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// index.js
/* const auth = require("./core/auth"); */
const initHajar = require("./core/init.js");

const hajar = {
/* auth: auth, */
initHajar,
version: "1.0.41",
};

module.exports = { hajar };
9 changes: 0 additions & 9 deletions www/src/@sikka/hajar/index.ts

This file was deleted.

0 comments on commit 9b4ed31

Please sign in to comment.