Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[47기 하준수] Add: 어드민유저 등록을 위한 기능 및 제반사항 추가 #19

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions api/controllers/adminUserController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const { adminUserService } = require("../services");

const adminUserSignUp = async (req, res) => {
try {
const { accountName, password, personalCode, name, email, phoneNumber } =
req.body;

if (
!accountName ||
!password ||
!personalCode ||
!name ||
!email ||
!phoneNumber
) {
const error = new Error("KEY_ERROR");
error.statusCode = 400;
throw error;
}

await adminUserService.adminUserSignUp(
accountName,
password,
personalCode,
name,
email,
phoneNumber
);
return res.status(201).json({ message: "CREATE_ADMIN_USER_SUCCESS!" });
} catch (error) {
console.log(error);
return res.status(error.statusCode).json({ message: error.message });
}
};

module.exports = { adminUserSignUp };
2 changes: 2 additions & 0 deletions api/controllers/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
const userController = require("./userController");
const productController = require("./productController");
const cartController = require("./cartController");
const adminUserController = require("./adminUserController");

module.exports = {
userController,
productController,
cartController,
adminUserController,
};
95 changes: 95 additions & 0 deletions api/models/adminUserDao.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
const { appDataSource } = require("./dataSource");

const confirmInsiderByEmail = async (email) => {
const [confirmedInsider] = await appDataSource.query(
`
SELECT EXISTS (
SELECT id
FROM insider_emails
WHERE email = ?
) exist
`,
[email]
);
return !!parseInt(confirmedInsider.exist);
};

const adminUserExistsByAccountName = async (accountName) => {
const [adminUserExistsByAccountName] = await appDataSource.query(
`
SELECT EXISTS (
SELECT *
joonsooha123 marked this conversation as resolved.
Show resolved Hide resolved
FROM admin_users
WHERE account_name = ?
) exist
`,
[accountName]
);
return adminUserExistsByAccountName;
joonsooha123 marked this conversation as resolved.
Show resolved Hide resolved
};

const adminUserExistsByEmail = async (email) => {
const [adminUserExistsByEmail] = await appDataSource.query(
`
SELECT EXISTS (
SELECT *
joonsooha123 marked this conversation as resolved.
Show resolved Hide resolved
FROM admin_users
WHERE email = ?
) exist
`,
[email]
);
return adminUserExistsByEmail;
};

const adminUserExistsByPhoneNumber = async (phoneNumber) => {
const [adminUserExistsByPhoneNumber] = await appDataSource.query(
`
SELECT EXISTS (
SELECT *
joonsooha123 marked this conversation as resolved.
Show resolved Hide resolved
FROM admin_users
WHERE phone_number = ?
) exist
`,
[phoneNumber]
);
return adminUserExistsByPhoneNumber;
};

const createAdminUser = async (
accountName,
hashedPassword,
personalCode,
name,
email,
phoneNumber
) => {
return await appDataSource.query(
`
INSERT INTO admin_users(
account_name,
password,
personal_code,
name,
email,
phone_number
) VALUES (
?,
?,
?,
?,
?,
?
);
`,
[accountName, hashedPassword, personalCode, name, email, phoneNumber]
);
};

module.exports = {
confirmInsiderByEmail,
adminUserExistsByAccountName,
adminUserExistsByEmail,
adminUserExistsByPhoneNumber,
createAdminUser,
};
2 changes: 2 additions & 0 deletions api/models/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ const dataSource = require("./dataSource");
const userDao = require("./userDao.js");
const productDao = require("./productDao");
const cartDao = require("./cartDao.js");
const adminUserDao = require("./adminUserDao");

module.exports = {
dataSource,
userDao,
productDao,
cartDao,
adminUserDao,
};
14 changes: 11 additions & 3 deletions api/models/productDao.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
const appDataSource = require("./dataSource");
const { appDataSource } = require("./dataSource");
const queryBuilder = require("./queryBuilder");

const getProductList = async (categoryIds, sorting, parsedLimit, parsedOffset) => {
const getProductList = async (
categoryIds,
sorting,
parsedLimit,
parsedOffset
) => {
try {
const filterQuery = queryBuilder.filterBuilder(categoryIds);
const orderQuery = queryBuilder.sortingBuilder(sorting);
const paginationQuery = queryBuilder.paginationBuilder(parsedLimit, parsedOffset);
const paginationQuery = queryBuilder.paginationBuilder(
parsedLimit,
parsedOffset
);
const productList = await appDataSource.query(
`
SELECT
Expand Down
8 changes: 8 additions & 0 deletions api/routes/adminUserRouter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const express = require("express");
const { adminUserController } = require("../controllers");

const router = express.Router();

router.post("/signup", adminUserController.adminUserSignUp);

module.exports = router;
2 changes: 2 additions & 0 deletions api/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ const router = express.Router();
const userRouter = require("./userRouter");
const productRouter = require("./productRouter");
const cartRouter = require("./cartRouter");
const adminUserRouter = require("./adminUserRouter");

router.use("/users", userRouter);
router.use("/products", productRouter);
router.use("/carts", cartRouter);
router.use("/adminUsers", adminUserRouter);

module.exports = router;
84 changes: 84 additions & 0 deletions api/services/adminUserService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
const bcrypt = require("bcrypt");

const { adminUserDao } = require("../models");

const adminUserSignUp = async (
accountName,
password,
personalCode,
name,
email,
phoneNumber
) => {
const confirmedInsider = await adminUserDao.confirmInsiderByEmail(email);

if (!confirmedInsider) {
const error = new Error("INVALID_APPROACH");
error.statusCode = 401;
throw error;
}

const saltRounds = 12;

const accountNameRegEx = /^(?=.*[a-zA-Z])(?=.*[0-9])[a-zA-Z0-9]+$/;
const passwordRegEx =
/^(?=.*[a-z])(?=.*[!@#$%^&*()-=_+])[a-zA-Z\d!@#$%^&*()-=_+]{10,16}$/;
const personalCodeRegEx = /^\d{6}$/;
const emailRegEx =
/^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/;

const accountNameExist = await adminUserDao.adminUserExistsByAccountName(
accountName
);
const emailExist = await adminUserDao.adminUserExistsByEmail(email);
const phoneNumberExist = await adminUserDao.adminUserExistsByPhoneNumber(
phoneNumber
);

if (!accountNameRegEx.test(accountName)) {
const error = new Error("INVALID_ACCOUNT_NAME");
error.statusCode = 400;
throw error;
}
if (!passwordRegEx.test(password)) {
const error = new Error("INVALID_PASSWORD");
error.statusCode = 400;
throw error;
}
if (!personalCodeRegEx.test(personalCode)) {
const error = new Error("INVALID_PERSONAL_CODE");
error.statusCode = 400;
throw error;
}

if (accountNameExist.exist > 0) {
const error = new Error("ACCOUNT_NAME_EXIST");
error.statusCode = 409;
throw error;
}
if (emailExist.exist > 0) {
const error = new Error("EMAIL_EXIST");
error.statusCode = 409;
throw error;
}
if (phoneNumberExist.exist > 0) {
const error = new Error("PHONE_NUMBER_EXIST");
error.statusCode = 409;
throw error;
}

const hashedPassword = await bcrypt.hash(password, saltRounds);

return await adminUserDao.createAdminUser(
accountName,
hashedPassword,
personalCode,
name,
email,
phoneNumber
);
};

module.exports = {
adminUserSignUp,
};
2 changes: 2 additions & 0 deletions api/services/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
const userService = require("./userService");
const productService = require("./productService");
const cartService = require("./cartService");
const adminUserService = require("./adminUserService");

module.exports = {
userService,
productService,
cartService,
adminUserService,
};
16 changes: 16 additions & 0 deletions db/migrations/20230705060310_create_admin_users_table.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
-- migrate:up
CREATE TABLE admin_users (
id INT NOT NULL AUTO_INCREMENT,
account_name varchar(200) NOT NULL UNIQUE,
password VARCHAR(200) NOT NULL,
personal_code INT NOT NULL,
name VARCHAR(50) NOT NULL,
email VARCHAR(200) NOT NULL UNIQUE,
phone_number VARCHAR(50) NOT NULL UNIQUE,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP NULL ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (id)
)

-- migrate:down
DROP TABLE admin_users
9 changes: 9 additions & 0 deletions db/migrations/20230705071115_create_insider_emails_table.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-- migrate:up
CREATE TABLE insider_emails (
id INT NOT NULL AUTO_INCREMENT,
email VARCHAR(200) NOT NULL UNIQUE,
PRIMARY KEY (id)
)

-- migrate:down
DROP TABLE insider_emails