Finish register flow

development
Visoth 9 months ago
parent 65e9e9ff39
commit 89477bb0c2
  1. 2
      Config/config.env
  2. 2
      Config/upload.js
  3. 47
      Controller/Authentication.js
  4. 1
      Controller/Database.js
  5. 133
      Controller/Store.js
  6. 50
      Controller/Subscriptions.js
  7. 4
      Model/StoreModel.js
  8. 0
      Model/TD_DATA.js
  9. 4
      app.js
  10. 11
      package-lock.json
  11. 1
      package.json
  12. 8
      routes/authentication.js
  13. 21
      routes/store.js
  14. 9
      routes/subscription.js
  15. BIN
      upload/store/tmp_post/2024-07-16_09-24-43_Artboard 2.png
  16. BIN
      upload/user/tmp_post/2024-07-12_15-58-19_MB-POS.png
  17. BIN
      upload/user/tmp_post/2024-07-12_15-59-39_MB-POS.png
  18. BIN
      upload/user/tmp_post/2024-07-12_16-00-54_MB-POS.png
  19. BIN
      upload/user/tmp_post/2024-07-12_16-01-04_MB-POS.png
  20. BIN
      upload/user/tmp_post/2024-07-12_16-01-19_MB-POS.png
  21. BIN
      upload/user/tmp_post/2024-07-12_16-02-50_MB-POS.png
  22. BIN
      upload/user/tmp_post/2024-07-12_16-03-11_MB-POS.png
  23. BIN
      upload/user/tmp_post/2024-07-12_16-03-39_MB-POS.png
  24. BIN
      upload/user/tmp_post/2024-07-12_16-03-48_MB-POS.png
  25. BIN
      upload/user/tmp_post/2024-07-12_16-04-01_MB-POS.png
  26. BIN
      uploads/store_image/4308.jpg
  27. BIN
      uploads/store_image/4309.jpg
  28. BIN
      uploads/user_image/4308.jpg

@ -7,6 +7,8 @@ JWT_COOKIE_EXPIRE=30
APP_CODE=POS
STORE_IMAGE_LOCATION = uploads/store_image/
USER_IMAGE_LOCATION = uploads/user_image/
ADMIN_USERNAME=bcsa
ADMIN_PASS=Bc@dmin

@ -1,7 +1,7 @@
const multer = require("multer");
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, "uploads");
cb(null, "upload");
},
filename: function (req, file, cb) {
cb(null, Date.now() + "-" + file.originalname);

@ -5,13 +5,9 @@ const AppModel = require("../Model/AppModel");
const ErrorResponse = require("../utils/errorResponse");
const dbInfo = require("../Model/Database")
const StoreInfo = require("../Model/StoreModel")
const DataModel = require("../Model/BC_Data");
const multer = require("multer");
var storage = multer.diskStorage({
destination: function (req, file, cb) {
if (file.fieldname == "photo") cb(null, "upload/user/tmp_post")
}
})
const DataModel = require("../Model/TD_DATA");
const UserModel = require("../Model/User");
const sequelize = require("sequelize");
// Login User
exports.login = asyncHandler(async (req, res, next) => {
@ -147,7 +143,10 @@ exports.register = asyncHandler(async (req, res, next) => {
try {
var data = JSON.parse(req.body.data)
console.log("DATA ", data)
const photo = req.files.photo
const { testing } = data
console.log("TESTING ", testing);
// const { username, password, email, phone, userType, appCode, dbCode, custCode } = req.body
res.status(200).send({
success: true,
@ -229,7 +228,6 @@ exports.checkUsername = asyncHandler(async (req, res, next) => {
}
// // check user type
log("USER TYPE ", user.USER_TYPE)
if (user.USER_TYPE == "S" || user.USER_TYPE == "C") {
var userPresets = await DataModel.findAll({
where: {
@ -250,8 +248,41 @@ exports.checkUsername = asyncHandler(async (req, res, next) => {
return next(new ErrorResponse("Username is not available", 404))
}
} catch (e) {
if (req.files && req.files.store_image) {
const filePath = req.files.store_image[0].path;
fs.unlink(filePath, err => {
if (err) console.error("Error removing file", err);
});
}
throw new ErrorResponse(e, 400)
}
})
exports.checkExistingUsername = asyncHandler(async (req, res, next) => {
const { username } = req.query
try {
const existingUsername = await UserModel.findOne({
where: {
USER_NAME: sequelize.where(
sequelize.fn("LOWER", sequelize.col("USER_NAME")),
" = ", username.toLowerCase()
)
},
order: [["USER_ID"]]
})
if (existingUsername) {
return res.status(200).send({
success: true,
data: true
})
}
return res.status(200).send({
success: true,
data: false
})
} catch (err) {
console.log("ERROR CHECK EXISTING USERNAME", err)
return next(new ErrorResponse(err, 400));
}
})

@ -1,7 +1,6 @@
const asyncHandler = require("../Middleware/async");
const sequelize = require('sequelize')
const DBInfo = require("../Model/Database");
const { log } = require("winston");
exports.getDBInfo = asyncHandler(async (req, res, next) => {
console.log("GET DB INFO EXEC ")

@ -1,9 +1,13 @@
const asyncHandler = require("../Middleware/async");
const sequelize = require('sequelize')
const StoreModel = require("../Model/StoreModel");
const UserModel = require("../Model/User");
const DataModel = require("../Model/TD_DATA");
const AppModel = require("../Model/AppModel");
const db = require("../Config/db");
const ErrorResponse = require("../utils/errorResponse");
const SuccessResponse = require("../utils/successResponse");
const fs = require('fs');
const dateFormat = require("dateformat");
@ -26,17 +30,53 @@ exports.getStoreInfo = asyncHandler(async (req, res, next) => {
})
exports.registerStore = asyncHandler(async (req, res, next) => {
const { store_name } = req.body
var data = JSON.parse(req.body.data)
console.log("REQUEST DATA ", data)
var photo = req.file.path
let imageData = fs.readFileSync(req.file.path)
console.log("PHOTO ", photo)
const { store_name, username, password, first_name, last_name, phone_number, plan_id, map } = data
const t = await db.transaction()
try {
const t = await db.transaction()
if (!username || !password || !first_name || !last_name || !store_name || !phone_number) {
throw ("Please provide information!")
}
if (!plan_id) {
throw "Please provide subscription plan"
}
if (!photo) {
throw "Please provide store image!"
}
// check whether store name is existing
const existingStore = await StoreModel.findOne({
where: { ST_NAME: store_name }
})
if (existingStore) {
return next(new ErrorResponse("Store name already exists", 400));
throw "Store name already exists"
// return next(new ErrorResponse("Store name already exists", 400));
}
const existingUsername = await UserModel.findOne({
where: {
USER_NAME: sequelize.where(
sequelize.fn("LOWER", sequelize.col("USER_NAME")),
" = ", username.toLowerCase()
)
},
order: [["USER_ID"]]
})
if (existingUsername) {
throw "Username already exists"
// return next(new ErrorResponse("Username already exists", 400));
}
var date = dateFormat(new Date(), "mm/dd/yyyy");
const nextStoreCode = await generateNextStoreCode()
@ -53,22 +93,79 @@ exports.registerStore = asyncHandler(async (req, res, next) => {
SB_DECICMA: '2',
DEC_SEP: '.',
THO_SEP: ',',
USER_CREA: req.user.USER_NAME,
USER_CREA: "",
USER_UPDT: '',
MAP: map
}, { transaction: t }
)
await createStoreTables(nextStoreCode, t);
// GET last user id
let lastUserID = await UserModel.findOne({
order: [['USER_ID', 'DESC']]
});
var newUserID = lastUserID.USER_ID + 1
let user = await UserModel.create({
USER_ID: newUserID,
USER_CODE: newUserID,
USER_CPAS: 0,
APPROVED: 1,
APPROVED_BY: "",
USER_NAME: username,
USER_PASS: UserModel.encryptPass(password),
FIRST_NAME: first_name,
LAST_NAME: last_name,
USER_DESC: last_name + " " + first_name,
USER_TYPE: 'A', // A for Admin in POS
USER_STATUS: '1',
USER_CREDATE: date,
FIELD_1: 'ADMIN',
FIELD_3: "ADMIN",
ROLE_TYPE: "ADMIN",
USER_LOG: ""
}, { transaction: t }
)
let subscriptionPlan = await DataModel.findOne({
where: { DATA_TYPE: 'POS_PLAN', ID: plan_id }
})
console.log("PLAN ID ", subscriptionPlan)
if (!subscriptionPlan || !subscriptionPlan.DATA_CODE) {
throw 'Please provide valid subscription plan'
}
delete user.dataValues.USER_PASS
let folderPath = process.env.STORE_IMAGE_LOCATION
if (!fs.existsSync(folderPath)) fs.mkdirSync(folderPath, {
recursive: true,
})
fs.writeFileSync(folderPath + newUserID + '.jpg', imageData)
fs.unlinkSync(req.file.path)
// INSERT INTO TDMSAPP
let app = AppModel.create({
USER_ID: newUserID,
APP_CODE: 'POS',
DB_CODE: nextStoreCode,
PRESET: subscriptionPlan.DATA_CODE,
CREATE_DATE: date
})
// await createStoreTables(nextStoreCode, t);
await t.commit();
res.status(201).json({
success: true,
data: store
data: store,
app: app
})
} catch (err) {
fs.unlinkSync(req.file.path)
await t.rollback();
return next(new ErrorResponse(err, 400));
}
})
@ -318,5 +415,27 @@ async function createStoreTables(storeCode, transaction) {
}
}
async function createUser(username, password, storeCode, first_name, last_name,) {
var date = dateFormat(new Date(), "mm/dd/yyyy");
try {
user = await UserModel.create({
USER_NAME: username,
USER_PASS: UserModel.encryptPass(password),
first_name: first_name,
last_name: last_name,
description: last_name + " " + first_name,
USER_TYPE: 'A', // A for Admin in POS
USER_STATUS: '1',
USER_CREDATE: date,
FIELD_1: 'ADMIN',
FIELD_3: "ADMIN",
ROLE_TYPE: "ADMIN",
})
} catch (e) {
console.error('Error creating user:', e);
throw new Error('Failed to create user.');
}
}

@ -0,0 +1,50 @@
const asyncHandler = require("../Middleware/async");
const sequelize = require('sequelize')
const DataModel = require("../Model/TD_DATA");
exports.getAllSubscriptions = asyncHandler(async (req, res, next) => {
DataModel.findAll({
where: {
DATA_TYPE: "POS_PLAN"
},
order: [
[sequelize.literal('CAST([FIELD_2] AS FLOAT)'), 'ASC'] // Sort by FIELD_2(Price) as integer
]
}).then((data) => {
return res.status(200).send(JSON.stringify(
{
success: true,
data
}
));
});
})
exports.getSubscriptionByID = asyncHandler(async (req, res, next) => {
const { id } = req.query;
console.log("PLAN ID :", id)
DataModel.findOne({
where: {
ID: id,
DATA_TYPE: "POS_PLAN"
},
order: [
[sequelize.literal('CAST([FIELD_2] AS FLOAT)'), 'ASC'] // Sort by FIELD_2(Price) as integer
]
}).then((data) => {
console.log("GET SUBSCRIPTION BY ID", data.DATA_CODE)
return res.status(200).send(JSON.stringify(
{
success: true,
data
}
));
});
})

@ -43,7 +43,11 @@ const StoreModel = db.define(
},
DATE_UPDT: {
type: sequelize.STRING
},
MAP: {
type: sequelize.STRING
}
},
{
timestamps: false,

@ -18,6 +18,7 @@ var authenticationRouter = require('./routes/authentication')
var dbInfoRouter = require('./routes/db')
var itemRouter = require('./routes/item')
var storeRouter = require('./routes/store')
var subscriptionRouter = require('./routes/subscription')
//CORS
@ -39,7 +40,7 @@ app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use("/upload", express.static("uploads"));
app.use("/upload", express.static("upload"));
app.use('/', indexRouter);
app.use('/users', usersRouter);
@ -49,6 +50,7 @@ app.use("/api/v1/authentication", cors(), authenticationRouter);
app.use("/api/v1/database", dbInfoRouter)
app.use("/api/v1/item", itemRouter)
app.use("/api/v1/store", storeRouter)
app.use("/api/v1/subscriptions", subscriptionRouter)

11
package-lock.json generated

@ -15,6 +15,7 @@
"debug": "~2.6.9",
"dotenv": "^16.4.5",
"express": "~4.16.1",
"faker": "^6.6.6",
"http-errors": "~1.6.3",
"jade": "~1.11.0",
"jsonwebtoken": "^9.0.2",
@ -1092,6 +1093,11 @@
"node": ">= 0.6"
}
},
"node_modules/faker": {
"version": "6.6.6",
"resolved": "https://registry.npmjs.org/faker/-/faker-6.6.6.tgz",
"integrity": "sha512-9tCqYEDHI5RYFQigXFwF1hnCwcWCOJl/hmll0lr5D2Ljjb0o4wphb69wikeJDz5qCEzXCoPvG6ss5SDP6IfOdg=="
},
"node_modules/fecha": {
"version": "4.2.3",
"resolved": "https://registry.npmjs.org/fecha/-/fecha-4.2.3.tgz",
@ -3482,6 +3488,11 @@
}
}
},
"faker": {
"version": "6.6.6",
"resolved": "https://registry.npmjs.org/faker/-/faker-6.6.6.tgz",
"integrity": "sha512-9tCqYEDHI5RYFQigXFwF1hnCwcWCOJl/hmll0lr5D2Ljjb0o4wphb69wikeJDz5qCEzXCoPvG6ss5SDP6IfOdg=="
},
"fecha": {
"version": "4.2.3",
"resolved": "https://registry.npmjs.org/fecha/-/fecha-4.2.3.tgz",

@ -13,6 +13,7 @@
"debug": "~2.6.9",
"dotenv": "^16.4.5",
"express": "~4.16.1",
"faker": "^6.6.6",
"http-errors": "~1.6.3",
"jade": "~1.11.0",
"jsonwebtoken": "^9.0.2",

@ -27,10 +27,8 @@ route.route("/appversion").get(appVersion.getAppVersion);
route.route("/checkusername").get(protectAtlogin, controller.checkUsername);
route.route("/register").post(upload.fields([
{
name: 'photo'
}
]), controller.register,);
route.route("/register").post(upload.single("user_image"), controller.register,);
route.route("/checkExistUsername").get(protectAtlogin, controller.checkExistingUsername);
module.exports = route;

@ -1,9 +1,26 @@
const express = require('express')
const route = express.Router()
const controller = require("../Controller/Store")
const { protect } = require("../Middleware/auth")
const { protect, protectAtlogin } = require("../Middleware/auth")
const multer = require('multer')
const moment = require('moment')
var storage = multer.diskStorage({
destination: function (req, file, cb) {
if (file.fieldname == "store_image") cb(null, "upload/store/tmp_post");
},
filename: function (req, file, cb) {
cb(
null,
moment(Date.now()).format("YYYY-MM-DD_HH-mm-ss_") + file.originalname
);
},
});
var upload = multer({ storage: storage });
route.route("/").get(protect, controller.getStoreInfo)
route.route("/register").post(protect, controller.registerStore)
route.route("/register").post(upload.single("store_image"), protectAtlogin, controller.registerStore)
module.exports = route;

@ -0,0 +1,9 @@
const express = require("express");
const route = express.Router();
const controller = require("../Controller/Subscriptions");
const { protectAtlogin } = require("../Middleware/auth");
route.route('/').get(protectAtlogin, controller.getAllSubscriptions)
route.route('/id').get(protectAtlogin, controller.getSubscriptionByID)
module.exports = route;

Binary file not shown.

After

Width:  |  Height:  |  Size: 102 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.1 MiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.1 MiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.1 MiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.1 MiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.1 MiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.1 MiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.1 MiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.1 MiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.1 MiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.1 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 586 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 586 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 742 KiB

Loading…
Cancel
Save