Skip to content

Commit

Permalink
Merge pull request #2 from fatihes1/feature/refactoring
Browse files Browse the repository at this point in the history
Refactoring Communication Services - via RPC added
  • Loading branch information
fatihes1 authored Apr 7, 2024
2 parents 7995b23 + 4024bbf commit 2f5db4c
Show file tree
Hide file tree
Showing 35 changed files with 946 additions and 493 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/CI_workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ jobs:
npm test
- name: Test Product Service
working-directory: ./product
working-directory: ./products
run: |
npm ci
npm test
- name: Test Shopping Service
working-directory: ./Shopping
working-directory: ./shopping
run: |
npm ci
npm test
8 changes: 5 additions & 3 deletions customer/.env.dev
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ APP_SECRET='jg_youtube_tutorial'

# Mongo DB
MONGODB_URI='mongodb://localhost:27017/microservices_customer'
#MESSAGE_BROKER_URL='amqp://localhost'
MESSAGE_BROKER_URL='amqp://localhost'
MESSAGE_BROKER_URL_PROD='amqps://aaticxba:[email protected]/aaticxba'
#MSG_QUEUE_URL='amqp://localhost'
MSG_QUEUE_URL='amqp://localhost'
MSG_QUEUE_URL_PROD='amqps://aaticxba:[email protected]/aaticxba'

EXCHANGE_NAME='ONLINE_STORE'

# Port
PORT=8005
16 changes: 0 additions & 16 deletions customer/src/api/app-events.js

This file was deleted.

99 changes: 85 additions & 14 deletions customer/src/api/customer.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
import CustomerService from "../services/customer-service.js";
import { authMiddleware } from "./middlewares/auth.js";
import { SubscribeMessage } from "../utils/index.js";
import { PublishMessage } from "../utils/index.js";
import config from "../config/index.js";
import {APIError} from "../utils/app-errors.js";

export default async function setupCustomerRoutes(app, channel) {
const service = new CustomerService();

await SubscribeMessage(channel, service)
// await SubscribeMessage(channel, service)

/**
* POST /signup
* This endpoint allows a new user to sign up.
* It expects an email, password, and phone number in the request body.
*
* @param {Object} req - The request object, expected to contain email, password, and phone in req.body.
* @param {Object} res - The response object.
* @param {Function} next - The next middleware function.
*
* @returns {Object} The data returned from the signUp service method.
*/
app.post("/signup", async (req, res, next) => {
try {
const { email, password, phone } = req.body;
Expand All @@ -17,6 +30,17 @@ export default async function setupCustomerRoutes(app, channel) {
}
});

/**
* POST /login
* This endpoint allows a user to log in.
* It expects an email and password in the request body.
*
* @param {Object} req - The request object, expected to contain email and password in req.body.
* @param {Object} res - The response object.
* @param {Function} next - The next middleware function.
*
* @returns {Object} The data returned from the signIn service method.
*/
app.post("/login", async (req, res, next) => {
try {
const { email, password } = req.body;
Expand All @@ -27,9 +51,22 @@ export default async function setupCustomerRoutes(app, channel) {
}
});

/**
* POST /address
* This endpoint allows an authenticated user to add a new address.
* It uses the authMiddleware to ensure that the request is authenticated.
* It expects a street, postal code, city, and country in the request body.
*
* @param {Object} req - The request object, expected to contain the authenticated user in req.user and the address details in req.body.
* @param {Object} res - The response object.
* @param {Function} next - The next middleware function.
*
* @returns {Object} The data returned from the addNewAddress service method.
*/
app.post("/address", authMiddleware, async (req, res, next) => {
try {
const { _id } = req.user;
console.log('ADDRESS CUSTOMERJS', _id,)
const { street, postalCode, city, country } = req.body;
const { data } = await service.addNewAddress(_id, {
street,
Expand All @@ -44,6 +81,17 @@ export default async function setupCustomerRoutes(app, channel) {
}
});

/**
* GET /profile
* This endpoint retrieves the profile of the authenticated user.
* It uses the authMiddleware to ensure that the request is authenticated.
*
* @param {Object} req - The request object, expected to contain the authenticated user in req.user.
* @param {Object} res - The response object.
* @param {Function} next - The next middleware function.
*
* @returns {Object} The profile of the authenticated user.
*/
app.get("/profile", authMiddleware, async (req, res, next) => {
try {
const { _id } = req.user;
Expand All @@ -54,23 +102,46 @@ export default async function setupCustomerRoutes(app, channel) {
}
});

app.get("/shopping-details", authMiddleware, async (req, res, next) => {
/**
* DELETE /profile
* This endpoint allows an authenticated user to delete their profile.
* It uses the authMiddleware to ensure that the request is authenticated.
*
* @param {Object} req - The request object, expected to contain the authenticated user in req.user.
* @param {Object} res - The response object.
* @param {Function} next - The next middleware function.
*
* @returns {Object} The data returned from the deleteProfile service method.
*/
app.delete("/profile", authMiddleware, async (req, res, next) => {
try {
const { _id } = req.user;
const { data } = await service.getShoppingDetails(_id);
const { data, payload } = await service.deleteProfile(_id);

// Publish message to shopping service
await PublishMessage(channel, config.SHOPPING_SERVICE, JSON.stringify(payload));
console.log('API: DATA:', data, ' PAYLOAD:', payload);
return res.json(data);
} catch (err) {
next(err);
console.log('API: ERROR:', err)
throw APIError('Data Not found', err);
}
});

app.get("/wishlist", authMiddleware, async (req, res, next) => {
try {
const { _id } = req.user;
const { data } = await service.getWishList(_id);
return res.status(200).json(data);
} catch (err) {
next(err);
}
});
/**
* GET /whoami
* This endpoint returns a message indicating the service identity.
* It does not require any authentication or parameters.
*
* @param {Object} req - The request object.
* @param {Object} res - The response object.
* @param {Function} next - The next middleware function.
*
* @returns {Object} A JSON object containing a message about the service identity.
*/
app.get("/whoami", (req, res, next) => {
return res.status(200).json({
msg: '/customer: I am a customer service'
})
})
}
2 changes: 0 additions & 2 deletions customer/src/api/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import customer from './customer.js'
import appEvents from './app-events.js'

export {
customer,
appEvents
}
8 changes: 4 additions & 4 deletions customer/src/config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ const config = {
PORT: process.env.PORT,
DB_URL: process.env.MONGODB_URI,
APP_SECRET: process.env.APP_SECRET,
MESSAGE_BROKER_URL: process.env.MESSAGE_BROKER_URL,
EXCHANGE_NAME: 'ONLINE_SHOPPING',
CUSTOMER_BINDING_KEY: 'CUSTOMER_SERVICE',
QUEUE_NAME: 'CUSTOMER_QUEUE'
EXCHANGE_NAME: process.env.EXCHANGE_NAME,
MSG_QUEUE_URL: process.env.MSG_QUEUE_URL,
CUSTOMER_SERVICE: "customer_service",
SHOPPING_SERVICE: "shopping_service",
};


Expand Down
2 changes: 1 addition & 1 deletion customer/src/database/models/Address.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ const AddressSchema = new Schema({
country: String
});

const Address = mongoose.model('Address', AddressSchema);
const Address = mongoose.model('address', AddressSchema);

export default Address;
28 changes: 0 additions & 28 deletions customer/src/database/models/Customer.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,6 @@ const CustomerSchema = new Schema({
address:[
{ type: Schema.Types.ObjectId, ref: 'address', require: true }
],
cart: [
{
product: {
_id: { type: String, require: true},
name: { type: String},
banner: { type: String},
price: { type: Number},
},
unit: { type: Number, require: true}
}
],
wishlist:[
{
_id: { type: String, require: true },
name: { type: String },
description: { type: String },
banner: { type: String },
available: { type: Boolean },
price: { type: Number}
}
],
orders: [
{
_id: { type: String, require: true },
amount: { type: String },
date: { type: Date, default: Date.now() }
}
]
},{
toJSON: {
transform(doc, ret){
Expand Down
Loading

0 comments on commit 2f5db4c

Please sign in to comment.