Beispiel #1
0
        public Response Post(string userId)
        {
            // check if the previous OTP is within time limit
            var lastOtp = otpDao.Get(userId);

            if (lastOtp != null)
            {
                TimeSpan ts = DateTime.UtcNow - DateTime.Parse(lastOtp.creationDate);
                if (ts.TotalMinutes < 5)
                {
                    response = Utility.Utils.SetResponse(response, false, Constant.ErrorCode.ERetryTime);
                    return(response);
                }
            }

            // disable all the previous OTP
            if (false == otpDao.DisableAll(userId))
            {
                response = Utility.Utils.SetResponse(response, false, Constant.ErrorCode.EGeneralError);
                return(response);
            }

            // add new otp to system
            string newOtp    = new Random().Next(100000, 999999).ToString();
            string newRefNum = Guid.NewGuid().ToString();

            if (false == otpDao.Add(userId, newOtp, newRefNum))
            {
                response = Utility.Utils.SetResponse(response, false, Constant.ErrorCode.EGeneralError);
                return(response);
            }

            // send to user's handphone
            var userData    = userDao.GetUserById(userId);
            var responseMsg = string.Format("Just Supply Chain Berhad.%0AYour OTP is: {0}. This OTP valid for 5 minutes.", newOtp);

            UtilSms.SendSms(userData.contactNumber, responseMsg);

            // TODO: generate email and send to user's email

            response.success      = true;
            response.errorCode    = Constant.ErrorCode.ESuccess;
            response.errorMessage = "Your temporary password was sent to your registered mobile phone.";

            return(response);
        }
Beispiel #2
0
        public Response Post([FromBody] Model.JobDetails jobDetails, string promoCode = null)
        {
            try
            {
                // first add the user if not existed
                var userId  = jobDetails.ownerUserId;
                var userObj = userDao.GetUserById(userId);
                if (userObj == null)
                {
                    response = Utility.Utils.SetResponse(response, false, Constant.ErrorCode.EParameterError);
                    return(response);
                }

                // validate the voucher
                if (promoCode != null)
                {
                    var voucherResult = new Vouchers();

                    // TODO: bug here: as the amount pass in was discounted amount, so it might below the expected use amount
                    // best way is not taking the amount required, and recalculate here to avoid injection attack
                    var responseCode = validateVoucher(promoCode, jobDetails.amount, out voucherResult);
                    if (responseCode != Constant.ErrorCode.ESuccess)
                    {
                        response = Utility.Utils.SetResponse(response, false, responseCode);
                        return(response);
                    }

                    if (voucherDao.IncreaseUsedCount(promoCode) == false)
                    {
                        DBLogger.GetInstance().Log(DBLogger.ESeverity.Warning, "voucherDao.IncreaseUsedCount(promoCode) in Common controller: " + promoCode);
                        response = Utility.Utils.SetResponse(response, false, Constant.ErrorCode.EVoucherNotValid);
                        return(response);
                    }
                }

                // get the gps coordinate if not passed in
                // get the state id and country id if not passed in
                foreach (Model.Address address in jobDetails.addressFrom)
                {
                    if (address.gpsLongitude == 0 ||
                        address.gpsLatitude == 0 ||
                        address.stateId == null ||
                        address.countryId == null)
                    {
                        // request gps cordinate
                        AddressComponents mapsObj = Utils.GetGpsCoordinate(address.address1, address.address2, address.address3, address.postcode);
                        if (mapsObj == null)
                        {
                            // find from local database
                            Postcode postcodeClass = new Postcode();
                            string   nameLocal;
                            var      result = postcodeClass.PostcodeNameList.TryGetValue(address.postcode, out nameLocal);
                            if (result == false)
                            {
                                response = Utility.Utils.SetResponse(response, false, Constant.ErrorCode.EGeneralError);
                                return(response);
                            }
                            mapsObj = Utils.GetGpsCoordinate(nameLocal);
                        }

                        if (address.gpsLongitude == 0)
                        {
                            address.gpsLongitude = mapsObj.geometry.location.lng;
                        }

                        if (address.gpsLatitude == 0)
                        {
                            address.gpsLatitude = mapsObj.geometry.location.lat;
                        }

                        if (address.countryId == null)
                        {
                            var countryObj = countryDao.GetCountries().Find(t => t.name.Contains(mapsObj.address_components.Find(c => c.types.Contains("country")).long_name));
                            address.countryId = countryObj.countryId;
                        }

                        if (address.stateId == null)
                        {
                            var stateList = stateDao.GetByCountryId(address.countryId);
                            try
                            {
                                var stateObj = stateList.Find(t => t.name.Contains(mapsObj.address_components.Find(a => a.types.Contains("administrative_area_level_1")).long_name));
                                if (stateObj == null)
                                {
                                    // cannot find from google api, use local database
                                    Postcode postcodeClass = new Postcode();
                                    string   stateLocal;
                                    var      localDic = postcodeClass.PostcodeList.TryGetValue(address.postcode, out stateLocal);
                                    address.stateId = stateList.Find(t => t.name.Contains(stateLocal)).stateId;
                                }
                                else
                                {
                                    address.stateId = stateObj.stateId;
                                }
                            }
                            catch (Exception)
                            {
                                // cannot find from google api, use local database
                                Postcode postcodeClass = new Postcode();
                                string   stateLocal;
                                var      localDic = postcodeClass.PostcodeList.TryGetValue(address.postcode, out stateLocal);
                                address.stateId = stateList.Find(t => t.name.Contains(stateLocal)).stateId;
                            }
                        }
                    }
                }

                if (jobDetails.addressTo == null)
                {
                    jobDetails.addressTo = new List <Model.Address>();
                }

                foreach (Model.Address address in jobDetails.addressTo)
                {
                    if (address.gpsLongitude == 0 ||
                        address.gpsLatitude == 0 ||
                        address.stateId == null ||
                        address.countryId == null)
                    {
                        // request gps cordinate
                        AddressComponents mapsObj = Utils.GetGpsCoordinate(address.address1, address.address2, address.address3, address.postcode);
                        if (mapsObj == null)
                        {
                            // find from local database
                            Postcode postcodeClass = new Postcode();
                            string   nameLocal;
                            var      result = postcodeClass.PostcodeNameList.TryGetValue(address.postcode, out nameLocal);
                            if (result == false)
                            {
                                response = Utility.Utils.SetResponse(response, false, Constant.ErrorCode.EGeneralError);
                                return(response);
                            }
                            mapsObj = Utils.GetGpsCoordinate(nameLocal);
                        }

                        if (address.gpsLongitude == 0)
                        {
                            address.gpsLongitude = mapsObj.geometry.location.lng;
                        }

                        if (address.gpsLatitude == 0)
                        {
                            address.gpsLatitude = mapsObj.geometry.location.lat;
                        }

                        if (address.countryId == null)
                        {
                            var countryObj = countryDao.GetCountries().Find(t => t.name.Contains(mapsObj.address_components.Find(c => c.types.Contains("country")).long_name));
                            address.countryId = countryObj.countryId;
                        }

                        if (address.stateId == null)
                        {
                            var stateList = stateDao.GetByCountryId(address.countryId);

                            try
                            {
                                var stateObj = stateList.Find(t => t.name.Contains(mapsObj.address_components.Find(a => a.types.Contains("administrative_area_level_1")).long_name));
                                if (stateObj == null)
                                {
                                    // cannot find from google api, use local database
                                    Postcode postcodeClass = new Postcode();
                                    string   stateLocal;
                                    postcodeClass.PostcodeList.TryGetValue(address.postcode, out stateLocal);
                                    address.stateId = stateList.Find(t => t.name.Contains(stateLocal)).stateId;
                                }
                                else
                                {
                                    address.stateId = stateObj.stateId;
                                }
                            }
                            catch (Exception)
                            {
                                // cannot find from google api, use local database
                                Postcode postcodeClass = new Postcode();
                                string   stateLocal;
                                postcodeClass.PostcodeList.TryGetValue(address.postcode, out stateLocal);
                                address.stateId = stateList.Find(t => t.name.Contains(stateLocal)).stateId;
                            }
                        }
                    }
                }

                // handle if partner amount is not present
                bool notifyPartners = false;
                if (jobDetails.amountPartner == 0)
                {
                    if (jobDetails.jobTypeId == ((int)JustApi.Constants.Configuration.DeliveryJobType.Standard).ToString())
                    {
                        // standard delivery

                        // find the partner price for distance in this distance
                        StandardDeliveryController tempController = new StandardDeliveryController();
                        var priceDetails = tempController.GetPrice(jobDetails.distance.ToString(), jobDetails.fleetTypeId,
                                                                   jobDetails.addressFrom[0].buildingType, jobDetails.addressTo[0].buildingType, jobDetails.workerAssistant.ToString(),
                                                                   jobDetails.assembleBed.ToString(), jobDetails.assembleDiningTable.ToString(), jobDetails.assembleWardrobe.ToString(),
                                                                   jobDetails.assembleOfficeTable.ToString(),
                                                                   jobDetails.bubbleWrapping.ToString(), jobDetails.shrinkWrapping.ToString());

                        jobDetails.amountPartner = priceDetails.partnerTotal;
                    }
                    else if (jobDetails.jobTypeId == ((int)JustApi.Constants.Configuration.DeliveryJobType.Disposal).ToString())
                    {
                        // disposal
                        DisposalDeliveryController tempController = new DisposalDeliveryController();
                        var priceDetails = tempController.GetPrice(jobDetails.fleetTypeId, jobDetails.addressFrom[0].buildingType, promoCode);

                        jobDetails.amountPartner = priceDetails.partnerTotal;
                    }
                }
                else
                {
                    // only when admin add job then push notification
                    // else notification push when user pay using payment gateway
                    notifyPartners = true;
                }

                // add the job details
                jobDetails.createdBy  = userId;
                jobDetails.modifiedBy = userId;
                var jobId = jobDetailsDao.Add(jobDetails);
                if (jobId == null)
                {
                    response = Utility.Utils.SetResponse(response, false, Constant.ErrorCode.EGeneralError);
                    return(response);
                }

                // add the job status
                if (null == jobDetailsDao.AddOrder(jobId, userId))
                {
                    response = Utility.Utils.SetResponse(response, false, Constant.ErrorCode.EGeneralError);
                    return(response);
                }

                // add the address from, to
                foreach (Model.Address add in jobDetails.addressFrom)
                {
                    add.createdBy = userId;
                    var result = addressDao.Add(add, jobId, userObj.displayName, userObj.contactNumber, Dao.AddressDao.EType.From);
                    if (result == null)
                    {
                        response = Utility.Utils.SetResponse(response, false, Constant.ErrorCode.EGeneralError);
                        return(response);
                    }
                }

                foreach (Model.Address add in jobDetails.addressTo)
                {
                    add.createdBy = userId;
                    var result = addressDao.Add(add, jobId, userObj.displayName, userObj.contactNumber, Dao.AddressDao.EType.To);
                    if (result == null)
                    {
                        response = Utility.Utils.SetResponse(response, false, Constant.ErrorCode.EGeneralError);
                        return(response);
                    }
                }

                // generate the unique job id
                var uniqueId = Utils.EncodeUniqueId(jobId);

                // request the job payment
                PaymentController controller = new PaymentController();
                var paymentReq = controller.Post(uniqueId);

                // send notification to creator
                var clientIdentifiers = userDao.GetDeviceIdentifier(userId);
                var msg = NotificationMsg.NewJob_Desc + uniqueId;
                if (clientIdentifiers != null &&
                    clientIdentifiers.Count != 0)
                {
                    // user have app installed and identifier found, send push notification
                    var extraData = Helper.PushNotification.ConstructExtraData(Helper.PushNotification.ECategories.OrderCreated, uniqueId);
                    Utility.UtilNotification.BroadCastMessage(clientIdentifiers.ToArray(), extraData, NotificationMsg.NewJob_Title, msg);
                }

                if (ConfigurationManager.AppSettings.Get("Debug") != "0")
                {
                    // send sms together because no history of push notification
                    UtilSms.SendSms(userObj.contactNumber, msg);
                }

                // send email to user
                var fleetType = fleetTypeDao.Get(jobDetails.fleetTypeId);
                var jobType   = jobTypeDao.Get().Find(t => t.jobTypeId == jobDetails.jobTypeId);
                UtilEmail.SendInvoice(uniqueId, (string)paymentReq.payload, userObj, jobDetails, fleetType.name, jobType.name);

                if (notifyPartners)
                {
                    // update the job order status
                    if (false == jobDeliveryDao.UpdateJobStatus(jobId, ((int)Constants.Configuration.JobStatus.PaymentVerifying).ToString()))
                    {
                        DBLogger.GetInstance().Log(DBLogger.ESeverity.Critical, string.Format("Unable to update job status. Job id: {0}", jobId));
                    }

                    // send notification to partners
                    var extraDataPartner       = Helper.PushNotification.ConstructExtraData(Helper.PushNotification.ECategories.NewOpenJob, jobId);
                    var partnerListIdentifiers = userDao.GetUserIdentifiersByRoleId(((int)Constants.Configuration.Role.CompanyAdmin).ToString());
                    if (int.Parse(jobDetails.jobTypeId) == (int)Constants.Configuration.DeliveryJobType.Standard)
                    {
                        Utility.UtilNotification.BroadCastMessage(
                            partnerListIdentifiers.ToArray(),
                            extraDataPartner,
                            NotificationMsg.NewOpenJob_Title,
                            NotificationMsg.NewOpenJob_Desc + string.Format("From: {0}\nTo: {1}\nAmount:{2}",
                                                                            jobDetails.addressFrom[0].address3,
                                                                            jobDetails.addressTo[0].address3,
                                                                            jobDetails.amountPartner)
                            );
                    }
                    else if (int.Parse(jobDetails.jobTypeId) == (int)Constants.Configuration.DeliveryJobType.Disposal)
                    {
                        Utility.UtilNotification.BroadCastMessage(
                            partnerListIdentifiers.ToArray(),
                            extraDataPartner,
                            NotificationMsg.NewOpenJob_Title,
                            NotificationMsg.NewOpenJob_Desc + string.Format("Dispose items from: {0}\nAmount:{1}",
                                                                            jobDetails.addressFrom[0].address3,
                                                                            jobDetails.amountPartner)
                            );
                    }
                }

                response.payload = uniqueId;
                response         = Utility.Utils.SetResponse(response, true, Constant.ErrorCode.ESuccess);

                return(response);
            }
            catch (Exception e)
            {
                DBLogger.GetInstance().Log(DBLogger.ESeverity.Error, e.Message);
                DBLogger.GetInstance().Log(DBLogger.ESeverity.Warning, e.StackTrace);

                response = Utility.Utils.SetResponse(response, false, Constant.ErrorCode.EUnknownError);
                return(response);
            }
        }