Skip to content

Commit

Permalink
👌 IMPROVE: added customers
Browse files Browse the repository at this point in the history
  • Loading branch information
hyper-dot committed Oct 15, 2024
1 parent d721c7e commit 4cc31b3
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 12 deletions.
7 changes: 4 additions & 3 deletions src/app/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,15 @@ export class AuthService {
refreshAccessToken(refreshToken: string) {
try {
const user: any = this.verifyRefreshToken(refreshToken);
return this.generateAccessToken({
const token = this.generateAccessToken({
id: user.id,
email: user.email,
role: user.role || 'user',
});
console.log('TOKEN', token);
return token;
} catch (err) {
console.log(err);
throw new BadRequestError('Could not refresh access token');
throw new BadRequestError(err);
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/app/sales/customer/customer.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ export default class CustomerController {
constructor() {
this.service = new CustomerService();
}

getAllCustomers = asyncWrapper(async (req, res) => {
const data = await this.service.getAllCustomers(req.userId);
return res.json({ message: 'Customers fetched successfully', data });
});
addNewCustomer = asyncWrapper(async (req, res) => {
const data = await this.service.addCustomer(req.body, req.userId);
return res.json({ message: 'Customer added successfully', data });
Expand Down
19 changes: 11 additions & 8 deletions src/app/sales/customer/customer.model.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { Schema, model } from 'mongoose';

const customerSchema = new Schema({
user: { type: Schema.ObjectId, required: true },
name: { type: String, required: true },
phone: { type: String, required: true },
email: { type: String, required: true },
dueAmount: { type: Number },
regNo: { type: String },
});
const customerSchema = new Schema(
{
user: { type: Schema.ObjectId, required: true },
name: { type: String, required: true },
phone: { type: String, required: true },
email: { type: String, required: true },
dueAmount: { type: Number },
regNo: { type: String },
},
{ timestamps: true },
);

const CustomerModel = model('Customer', customerSchema);
export default CustomerModel;
1 change: 1 addition & 0 deletions src/app/sales/customer/customer.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export class CustomerRoute {
this.mountRoutes();
}
mountRoutes() {
this.router.get('/', this.controller.getAllCustomers);
this.router.post('/', this.controller.addNewCustomer);
}
}
Expand Down
6 changes: 5 additions & 1 deletion src/app/sales/customer/customer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@ import { BadRequestError } from '../../../utils/exceptions';
import CustomerModel from './customer.model';

export default class CustomerService {
async getAllCustomers(user: string) {
return await CustomerModel.find({ user }).select('-user');
}

async addCustomer(payload: TCustomerSchema, user: string) {
const { data, success } = customerSchema.safeParse(payload);
if (!success) throw new BadRequestError('Invalid customer data');
const customer = await CustomerModel.create(data);
const customer = await CustomerModel.create({ ...data, user });
return customer;
}
}

0 comments on commit 4cc31b3

Please sign in to comment.