billingRecordController.js

mail@pastecode.io avatar
unknown
plain_text
a month ago
2.1 kB
0
Indexable
Never
const User = require('../Models/userModel')
const BillingRecord = require('../Models/billingRecordModel');

//Endpoint for Retrieving Billing Records
exports.getRecordsByEmail = async (req ,res) => {
    try {
        const searchEmail = req.query.searchEmail;
        let query = {};
        if (searchEmail) {
            query.email = searchEmail;
        }

        const agentName = await User.findOne({email: searchEmail, role: 'agent'}).select('name');
        if (agentName) {
            const billingRecords = await this.BillingRecord.find(query);

            res.status(200).send({
                "status": "success",
                "data": billingRecords.map(record => ({
                    id: record.id,
                    description: record.description,
                    agentName: record.agentName,
                    creationDate: record.creationDate,
                    amount: record.amount,
                    status: record.status

                }))
            })
        } else {
            res.send({
                "status": 'invalid',
                "message": 'there is no record for this agent'
            })
        }
        
    } catch (error) {
        res.status(400).send({
            "status": "error",
            "message": error.message
        })
    }
}

//Endpoint for Record Details
exports.getRecordsById = async (req , res) => {
    try {
        const billingRecords = await BillingRecord.findById(req.params.id);
        res.status(200).send({
            "status": 'success',
            'data': billingRecords.map(record => ({
                id: record.id,
                description: record.description,
                agentName: record.agentName,
                creationDate: record.creationDate,
                amount: record.amount,
                status: record.status
            
            }))
        })
    } catch (error) {
        res.status(400).send({
            "status": 'error',
            "message": error.message
        })
    }
}
Leave a Comment