You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
370 lines
9.3 KiB
370 lines
9.3 KiB
3 years ago
|
const fastify = require("fastify");
|
||
|
const userController = require("../controllers/userController");
|
||
|
const validationHandler = require("../handlers/userHandler");
|
||
|
|
||
|
module.exports = function (fastify, opts, next) {
|
||
|
fastify.get("/api/users", {
|
||
|
schema: {
|
||
|
tags: ["User"],
|
||
|
description: "This is for Get All Users",
|
||
|
summary: "This is for to Get All Users",
|
||
|
security: [
|
||
|
{
|
||
|
basicAuth: [],
|
||
|
},
|
||
|
],
|
||
|
},
|
||
|
preHandler: fastify.auth([fastify.authenticate]),
|
||
|
handler: userController.getUsers,
|
||
|
});
|
||
|
|
||
|
fastify.route({
|
||
|
method: "POST",
|
||
|
url: "/api/users/:username",
|
||
|
schema: {
|
||
|
description: "To Get user by username",
|
||
|
tags: ["User"],
|
||
|
summary: "This is for Get a Single User by Username",
|
||
|
params: {
|
||
|
type: "object",
|
||
|
properties: {
|
||
|
username: {
|
||
|
type: "string",
|
||
|
description: "username",
|
||
|
},
|
||
|
},
|
||
|
},
|
||
|
security: [
|
||
|
{
|
||
|
basicAuth: [],
|
||
|
},
|
||
|
],
|
||
|
},
|
||
|
preHandler: fastify.auth([fastify.authenticate]),
|
||
|
handler: userController.getSingleUser,
|
||
|
});
|
||
|
|
||
|
fastify.route({
|
||
|
method: "POST",
|
||
|
url: "/api/currentUser",
|
||
|
schema: {
|
||
|
tags: ["User"],
|
||
|
description: "This is for Get Current User by User Name by Post Body",
|
||
|
summary: "This is for Get a Current User.",
|
||
|
body: {
|
||
|
type: "object",
|
||
|
required: ["username"],
|
||
|
properties: {
|
||
|
username: { type: "string" },
|
||
|
},
|
||
|
},
|
||
|
security: [
|
||
|
{
|
||
|
basicAuth: [],
|
||
|
},
|
||
|
],
|
||
|
},
|
||
|
preHandler: fastify.auth([fastify.authenticate]),
|
||
|
handler: userController.getCurrentUser,
|
||
|
// onSend: (request, reply, done) => {
|
||
|
|
||
|
// // fire&forget
|
||
|
// request.log.info("#########################################");
|
||
|
// request.log.info(reply);
|
||
|
|
||
|
// done()
|
||
|
// },
|
||
|
});
|
||
|
|
||
|
fastify.route({
|
||
|
method: "PUT",
|
||
|
url: "/api/user/:userId",
|
||
|
schema: {
|
||
|
tags: ["User"],
|
||
|
summary: "This is for update user",
|
||
|
params: {
|
||
|
type: "object",
|
||
|
properties: {
|
||
|
userId: {
|
||
|
type: "string",
|
||
|
description: "user id",
|
||
|
},
|
||
|
},
|
||
|
},
|
||
|
body: {
|
||
|
type: "object",
|
||
|
// required: ['phone'],
|
||
|
properties: {
|
||
|
phone: { type: "string" },
|
||
|
firstName: { type: "string" },
|
||
|
lastName: { type: "string" },
|
||
|
address1: { type: "string" },
|
||
|
address2: { type: "string" },
|
||
|
city: { type: "string" },
|
||
|
state: { type: "string" },
|
||
|
country: { type: "string" },
|
||
|
zip: { type: "string" },
|
||
|
},
|
||
|
},
|
||
|
security: [
|
||
|
{
|
||
|
basicAuth: [],
|
||
|
},
|
||
|
],
|
||
|
},
|
||
|
preHandler: [
|
||
|
fastify.auth([fastify.operatorAuthenticate]),
|
||
|
validationHandler.validatePhoneFormat,
|
||
|
],
|
||
|
// preHandler: fastify.auth([fastify.authenticate]),
|
||
|
handler: userController.editUserInfo,
|
||
|
});
|
||
|
|
||
|
fastify.route({
|
||
|
method: "POST",
|
||
|
url: "/api/users",
|
||
|
schema: {
|
||
|
tags: ["User"],
|
||
|
description: "This is for cretae New user",
|
||
|
summary: "This is for Create New User.",
|
||
|
body: {
|
||
|
type: "object",
|
||
|
properties: {
|
||
|
username: { type: "string" },
|
||
|
password: { type: "string" },
|
||
|
emails: {
|
||
|
type: "array",
|
||
|
maxItems: 2,
|
||
|
items: {
|
||
|
type: "object",
|
||
|
properties: {
|
||
|
email: { type: "string", default: null },
|
||
|
},
|
||
|
},
|
||
|
},
|
||
|
firstName: { type: "string", default: null },
|
||
|
lastName: { type: "string", default: null },
|
||
|
phone: { type: "string" },
|
||
|
address1: { type: "string", default: null },
|
||
|
address2: { type: "string", default: null },
|
||
|
city: { type: "string", default: null },
|
||
|
state: { type: "string", default: null },
|
||
|
zip: { type: "string", default: null },
|
||
|
country: { type: "string", default: null },
|
||
|
notes: { type: "string", default: null },
|
||
|
},
|
||
|
},
|
||
|
security: [
|
||
|
{
|
||
|
basicAuth: [],
|
||
|
},
|
||
|
],
|
||
|
},
|
||
|
preHandler: [
|
||
|
validationHandler.fieldCheck,
|
||
|
validationHandler.verifyUser,
|
||
|
// validationHandler.validatePhoneFormat,
|
||
|
validationHandler.validateEmailFormat,
|
||
|
],
|
||
|
handler: userController.addUser,
|
||
|
// onResponse: (request, reply) => {
|
||
|
// validationHandler.sendPhoneVerificationCode(request, reply);
|
||
|
// },
|
||
|
//onResponse: validationHandler.sendPhoneVerificationCode,
|
||
|
});
|
||
|
|
||
|
fastify.route({
|
||
|
method: "POST",
|
||
|
url: "/api/phone",
|
||
|
schema: {
|
||
|
tags: ["User"],
|
||
|
description: "This is for verify User Phone",
|
||
|
summary: "This is to Verify User Phone.",
|
||
|
body: {
|
||
|
type: "object",
|
||
|
required: ["phone"],
|
||
|
properties: {
|
||
|
phoneVerificationCode: { type: "string" },
|
||
|
phone: { type: "string" },
|
||
|
},
|
||
|
},
|
||
|
security: [
|
||
|
{
|
||
|
basicAuth: [],
|
||
|
},
|
||
|
],
|
||
|
},
|
||
|
// preHandler: fastify.auth([fastify.authenticate]),
|
||
|
handler: validationHandler.verifyPhone,
|
||
|
});
|
||
|
|
||
|
fastify.route({
|
||
|
method: "POST",
|
||
|
url: "/api/forgotpassword",
|
||
|
schema: {
|
||
|
tags: ["User"],
|
||
|
description: "This is for forget password for the User.",
|
||
|
summary: "This is for forget User Password.",
|
||
|
body: {
|
||
|
type: "object",
|
||
|
required: ["phone"],
|
||
|
properties: {
|
||
|
phone: { type: "string" },
|
||
|
},
|
||
|
},
|
||
|
security: [
|
||
|
{
|
||
|
basicAuth: [],
|
||
|
},
|
||
|
],
|
||
|
},
|
||
|
// preHandler: [validationHandler.],
|
||
|
handler: validationHandler.forgotPassword,
|
||
|
onResponse: (request, reply) => {
|
||
|
validationHandler.sendPasswordResetCode(request, reply);
|
||
|
},
|
||
|
});
|
||
|
|
||
|
fastify.route({
|
||
|
method: "POST",
|
||
|
url: "/api/resetpassword",
|
||
|
schema: {
|
||
|
tags: ["User"],
|
||
|
description: "This is for Reset User Password.",
|
||
|
summary: "This is for Reset User Password.",
|
||
|
body: {
|
||
|
type: "object",
|
||
|
required: ["phone", "resetPasswordCode", "newPassword"],
|
||
|
properties: {
|
||
|
phone: { type: "string" },
|
||
|
resetPasswordCode: { type: "string" },
|
||
|
newPassword: { type: "string" },
|
||
|
},
|
||
|
},
|
||
|
security: [
|
||
|
{
|
||
|
basicAuth: [],
|
||
|
},
|
||
|
],
|
||
|
},
|
||
|
// preHandler: [validationHandler.],
|
||
|
handler: validationHandler.resetPassword,
|
||
|
// onResponse: (request,reply) => {validationHandler.resetPassword(request,reply)}
|
||
|
});
|
||
|
|
||
|
fastify.route({
|
||
|
method: "POST",
|
||
|
url: "/api/resetPasswordFromAdmin",
|
||
|
schema: {
|
||
|
tags: ["User"],
|
||
|
description: "This is for Reset Password for Admin.",
|
||
|
summary: "This is for Reset Password for Admin.",
|
||
|
body: {
|
||
|
type: "object",
|
||
|
properties: {
|
||
|
userId: { type: "string" },
|
||
|
},
|
||
|
},
|
||
|
security: [
|
||
|
{
|
||
|
basicAuth: [],
|
||
|
},
|
||
|
],
|
||
|
},
|
||
|
// preHandler: [validationHandler.],
|
||
|
handler: validationHandler.resetPasswordFromAdmin,
|
||
|
// onResponse: (request,reply) => {validationHandler.resetPassword(request,reply)}
|
||
|
});
|
||
|
|
||
|
fastify.route({
|
||
|
method: "POST",
|
||
|
url: "/api/resendphoneverificationcode",
|
||
|
schema: {
|
||
|
tags: ["User"],
|
||
|
description: "This is for Reset phone Verification Code.",
|
||
|
summary: "This is for Reset phone verification Code.",
|
||
|
body: {
|
||
|
type: "object",
|
||
|
required: ["phone"],
|
||
|
properties: {
|
||
|
phone: { type: "string" },
|
||
|
},
|
||
|
},
|
||
|
security: [
|
||
|
{
|
||
|
basicAuth: [],
|
||
|
},
|
||
|
],
|
||
|
},
|
||
|
// preHandler: [validationHandler.],
|
||
|
handler: validationHandler.sendPhoneVerificationCode,
|
||
|
// onResponse: (request,reply) => {validationHandler.sendPhoneVerificationCode(request,reply)}
|
||
|
});
|
||
|
|
||
|
fastify.route({
|
||
|
method: "POST",
|
||
|
url: "/api/users/send_message",
|
||
|
schema: {
|
||
|
tags: ["User"],
|
||
|
description: "This is for Send Message.",
|
||
|
summary: "This is for Send Message.",
|
||
|
body: {
|
||
|
type: "object",
|
||
|
required: ["userId"],
|
||
|
properties: {
|
||
|
userId: { type: "string" },
|
||
|
},
|
||
|
},
|
||
|
security: [
|
||
|
{
|
||
|
basicAuth: [],
|
||
|
},
|
||
|
],
|
||
|
},
|
||
|
handler: validationHandler.sendMessageNotification,
|
||
|
});
|
||
|
|
||
|
fastify.route({
|
||
|
method: "PUT",
|
||
|
url: "/api/update/currentUser/:username",
|
||
|
schema: {
|
||
|
tags: ["User"],
|
||
|
summary: "This is for update current user",
|
||
|
params: {
|
||
|
type: "object",
|
||
|
properties: {
|
||
|
username: {
|
||
|
type: "string",
|
||
|
description: "username",
|
||
|
},
|
||
|
},
|
||
|
},
|
||
|
body: {
|
||
|
type: "object",
|
||
|
properties: {
|
||
|
phone: { type: "string" },
|
||
|
firstName: { type: "string" },
|
||
|
lastName: { type: "string" },
|
||
|
address1: { type: "string" },
|
||
|
address2: { type: "string" },
|
||
|
city: { type: "string" },
|
||
|
state: { type: "string" },
|
||
|
country: { type: "string" },
|
||
|
zip: { type: "string" },
|
||
|
},
|
||
|
},
|
||
|
security: [
|
||
|
{
|
||
|
basicAuth: [],
|
||
|
},
|
||
|
],
|
||
|
},
|
||
|
preHandler: [fastify.auth([fastify.authenticate])],
|
||
|
handler: userController.editCuurentUserInfo,
|
||
|
});
|
||
|
// Login for a user is in the main index.js file.
|
||
|
// fastify-jwt used to create the token was throwing exceptions and requierd
|
||
|
// it be called before the route is loaded.
|
||
|
next();
|
||
|
};
|