-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
749b06a
commit 7940578
Showing
6 changed files
with
85 additions
and
62 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,80 @@ | ||
let initialized = false; | ||
let secret; | ||
let mongooseInstance; | ||
let User; | ||
let Admin; | ||
let Client; | ||
|
||
function initHajar( | ||
jwtSecret, | ||
inputMongooseInstance, | ||
mongooseInstance, | ||
userModel, | ||
adminModel, | ||
clientModel | ||
) { | ||
if (initialized) { | ||
throw new Error("Hajar is already initialized"); | ||
} | ||
let initialized = false; | ||
|
||
secret = jwtSecret; | ||
mongooseInstance = inputMongooseInstance; | ||
User = userModel; | ||
Admin = adminModel; | ||
Client = clientModel; | ||
initialized = true; | ||
} | ||
let User = userModel; | ||
let Admin = adminModel; | ||
let Client = clientModel; | ||
let secret = jwtSecret; | ||
let mongoose = mongooseInstance; | ||
|
||
async function getUserType(email) { | ||
const user = await User.findOne({ email }); | ||
if (!user) { | ||
throw new Error("User not found"); | ||
} | ||
return user.ref; | ||
} | ||
const initialize = () => { | ||
if (initialized) { | ||
throw new Error("Hajar is already initialized"); | ||
} | ||
|
||
initialized = true; | ||
console.log("Hajar initialized successfully."); | ||
}; | ||
|
||
async function getAdminData(user) { | ||
if (user.ref === "admin") { | ||
const adminData = await Admin.findOne({ uid: user._id }); | ||
if (!adminData) { | ||
throw new Error("Admin data not found"); | ||
const getUserType = async (email) => { | ||
if (!initialized) { | ||
throw new Error("Hajar is not initialized"); | ||
} | ||
return adminData; | ||
} | ||
return null; | ||
} | ||
const user = await User.findOne({ email }); | ||
if (!user) { | ||
throw new Error("User not found"); | ||
} | ||
return user.ref; | ||
}; | ||
|
||
async function getClientData(user) { | ||
if (user.ref === "client") { | ||
const clientData = await Client.findOne({ uid: user._id }); | ||
if (!clientData) { | ||
throw new Error("Client data not found"); | ||
const getAdminData = async (user) => { | ||
if (!initialized) { | ||
throw new Error("Hajar is not initialized"); | ||
} | ||
return clientData; | ||
} | ||
return null; | ||
if (user.ref === "admin") { | ||
const adminData = await Admin.findOne({ uid: user._id }); | ||
if (!adminData) { | ||
throw new Error("Admin data not found"); | ||
} | ||
return adminData; | ||
} | ||
return null; | ||
}; | ||
|
||
const getClientData = async (user) => { | ||
if (!initialized) { | ||
throw new Error("Hajar is not initialized"); | ||
} | ||
if (user.ref === "client") { | ||
const clientData = await Client.findOne({ uid: user._id }); | ||
if (!clientData) { | ||
throw new Error("Client data not found"); | ||
} | ||
return clientData; | ||
} | ||
return null; | ||
}; | ||
|
||
// Initialize hajar with external data | ||
initialize(); | ||
|
||
// Expose functions and models | ||
return { | ||
getUserType, | ||
getAdminData, | ||
getClientData, | ||
User, // Expose User model | ||
Admin, // Expose Admin model | ||
Client, // Expose Client model | ||
secret, // Expose secret | ||
mongoose, // Expose mongoose | ||
}; | ||
} | ||
|
||
export { | ||
initHajar, | ||
getUserType, | ||
getAdminData, | ||
getClientData, | ||
secret, | ||
mongooseInstance, | ||
User, | ||
Admin, | ||
Client, | ||
}; | ||
export { initHajar }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters