{"size": 4138, "ext": "js", "max_stars_repo_path": "api/handlers/client_address.js", "max_stars_repo_name": "e-Pharma/e-Pharma-Server", "max_stars_repo_stars_event_min_datetime": "2020-08-04T13:38:30.000Z", "max_stars_repo_stars_event_max_datetime": "2020-10-05T11:48:19.000Z", "max_issues_repo_path": "api/handlers/client_address.js", "max_issues_repo_name": "e-Pharma/e-Pharma-Server", "max_issues_repo_issues_event_min_datetime": "2020-06-28T16:43:37.000Z", "max_issues_repo_issues_event_max_datetime": "2022-01-13T02:54:42.000Z", "max_forks_repo_path": "api/handlers/client_address.js", "max_forks_repo_name": "e-Pharma/e-Pharma-Server", "max_forks_repo_forks_event_min_datetime": "2020-05-09T16:36:14.000Z", "max_forks_repo_forks_event_max_datetime": "2020-06-26T07:02:09.000Z"}
{"max_stars_count": 2, "max_issues_count": 11, "max_forks_count": 2, "avg_line_length": 30.4264705882, "max_line_length": 69, "alphanum_fraction": 0.5251329145}
const express = require("express");
const mongoose = require("mongoose");
const response = require("../utils/response");

const Address = require('../models/delivery_address');
const Client =require('../models/client')
const jwt = require("jsonwebtoken");
const jwtVerify = require("../handlers/verifyJWT")
const Logger = require("../utils/logger");
const logger = new Logger();

exports.getAddress=(req,res)=>{
    const token = req.headers['authorization'].slice(6);
    const isVerified = jwtVerify.verifyJWT(token);
    console.log(isVerified.data.id)
    if(isVerified.isTrue){
        Address.find({clientId: isVerified.data.id}, (err,data)=>{
            if(err){
                console.log(err);
                return response(res,null,500,err);
            }
            else{
                console.log(data);
                return response(res, data.items, 200, "Success");
            }
        })
    } else {
        logger.error("Error", isVerified)
        return response(res, null, 400, "Bad Request!")
    }
    // // if(isVerified.isTrue) {
    // //     const clientId = isVerified.data.id;
    // Address.findById(req.params.id,(err,data)=>{
    //         // {clientId:clientId}
    //         if(err){
    //             console.log(err);
    //             return response(res,null,500,err);
    //         }
    //         else{
    //             console.log(data);
    //             return response(res, data.items, 200, "Success");
    //         }
    //     })
    // // }else {
    // //     // logger.error(isVerified.isTrue);
    // //     return response(res, null, 400, "Bad Request");
    // //   }
}

exports.getAllAddresses = (req, res) => {
    const token = req.headers['authorization'].slice(6);
    console.log("TOK"+token)
    const isVerified = jwtVerify.verifyJWT(token);
    if(isVerified.isTrue) {
        const clientId = isVerified.data.id;
        console.log("CLIENT:"+clientId)
        Address.findOne({ clientId: clientId },(err,data)=>{
            if(err){
                console.log(err);
                return response(res,null,500,err);
            }
            else{
                console.log("DATA:"+data)
                if(data === null)
                    return response(res, undefined, 200, "Success");
                else
                    return response(res, data.items, 200, "Success");
            }
        });
    }else {
        logger.error(isVerified.isTrue);
        return response(res, null, 400, "Bad Request");
  }
}

/*add new address to the address book*/
exports.addNewAddress=(req,res)=>{
    /** Do not change. */
    const token = req.headers['authorization'].slice(6);
    const isVerified = jwtVerify.verifyJWT(token);

    if(isVerified.isTrue){
     console.log(req.body)

     Address.findOneAndUpdate({clientId: isVerified.data.id},
        // {$push:{items:item}},
        {$push:{items:req.body}},
       
        {safe:true,upsert:true},
        function(err,doc){
            if(err){
                console.log(err);
                return response(res,null,500,err);

            }else{
                console.log("Successfully added");
                return response(res, null, 200, "Success");
            }
        }
        )
    } else {
        console.log(err);
        return response(res,null,400,"Invalid Request");
    }

}

exports.deleteAddress=(req,res)=>{
    const docId={_id:req.params.id}
    Address.findByIdAndUpdate(docId,
        {$pull:{items:req.body.items}},
        {safe:true,upsert:true},
        function(err,doc){
            if(err){
                console.log(err);
                return response(res,null,500,err);

            }else{
                console.log("Deleted Successfully");
                return response(res, null, 200, "Success");
            }
        }
    )
}

exports.deleteAll = async(req, res)=>{
    Address.deleteMany({},(err,res1)=>{
        if(err){
            logger.error(err)
            return response(res, null, 500, "Server Error")
        } else {
            logger.info(res1)
            return response(res, res1, 200, "Success")
        }
    })
}