add user routes
This commit is contained in:
@@ -2,19 +2,25 @@ import mongoose from "mongoose";
|
||||
import { generateId, generateToken } from "../utils/id";
|
||||
import { CreateUserInput, UpdateUserInput, userModel } from "./user.schema";
|
||||
import { sendMail } from "../utils/mail";
|
||||
import { AuthenticatedUser } from "../auth";
|
||||
|
||||
export async function createUser(input: CreateUserInput, tenantId: string) {
|
||||
export async function createUser(
|
||||
input: CreateUserInput,
|
||||
user: AuthenticatedUser
|
||||
) {
|
||||
const token = await generateToken();
|
||||
|
||||
const user = await userModel.create({
|
||||
tenantId: tenantId,
|
||||
const newUser = await userModel.create({
|
||||
tenantId: user.tenantId,
|
||||
pid: generateId(),
|
||||
name: input.firstName + " " + input.lastName,
|
||||
createdAt: new Date(),
|
||||
createdBy: user.userId,
|
||||
token: {
|
||||
value: token,
|
||||
expiry: new Date(Date.now() + 3600 * 6 * 1000),
|
||||
},
|
||||
status: "invited",
|
||||
...input,
|
||||
});
|
||||
|
||||
@@ -22,11 +28,15 @@ export async function createUser(input: CreateUserInput, tenantId: string) {
|
||||
input.email,
|
||||
"You have been invited to Quicker Permtis.",
|
||||
`Click <a href="${
|
||||
process.env.SERVER_DOMAIN + "/auth/webauthn/register?token=" + token
|
||||
process.env.SERVER_DOMAIN +
|
||||
"/auth/webauthn/register?token=" +
|
||||
token +
|
||||
"&email=" +
|
||||
newUser.email
|
||||
}">here</a> to register.`
|
||||
);
|
||||
|
||||
return user;
|
||||
return newUser;
|
||||
}
|
||||
|
||||
export async function getUser(userId: string) {
|
||||
@@ -47,6 +57,26 @@ export async function getUserByEmail(email: string) {
|
||||
return await userModel.findOne({ email: email });
|
||||
}
|
||||
|
||||
export async function updateUser(userId: string, input: UpdateUserInput) {
|
||||
return await userModel.findOneAndUpdate({ pid: userId }, input);
|
||||
export async function listUsers(tenantId: string) {
|
||||
return await userModel
|
||||
.find({ tenantId: tenantId })
|
||||
.select(
|
||||
"_id pid orgId firstName lastName name email role avatar status createdAt createdBy lastLogin"
|
||||
);
|
||||
}
|
||||
|
||||
export async function updateUser(userId: string, input: UpdateUserInput) {
|
||||
return await userModel
|
||||
.findOneAndUpdate({ pid: userId }, input, {
|
||||
new: true,
|
||||
})
|
||||
.select(
|
||||
"_id pid orgId firstName lastName name email role avatar status createdAt createdBy lastLogin"
|
||||
);
|
||||
}
|
||||
|
||||
export async function deleteUser(userId: string, tenantId: string) {
|
||||
return await userModel.deleteOne({
|
||||
$and: [{ pid: userId }, { tenantId: tenantId }],
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user