Introduction Last updated: 09-NOV-2020 @ 07:00 PM

ZaiServe on premise application.

Environment Setup

Server & Local environment setup

Server Setup

Login to the DOMAIN Provider

Here we using Godaddy

  • https://account.godaddy.com/products
  • Navigate to All Products and Services > Domains
  • Search for the domain name(eg.zaicrm.com) & click the DNS link next to it and update the A Record simailar to the below one.
Type Name Value TTL
A @ 94.177.199.10 600 seconds
A * 94.177.199.10 1/2 Hour

Note: IP Address below the Value column is the Server IP.

Login to the SERVER with root access

Here we using Aruba Cloud CentOS(Server IP - 94.177.199.10)

  • Naviagate to /var/www/html & create a new folder same as the domain name(eg.zaicrm.com)
  • Naviagate to /etc/httpd/conf.d/ & create a new file with extension .conf(eg.zaicrm.conf) and add the following
<VirtualHost *:80>
    DocumentRoot /var/www/html/zaicrm.com
    ServerName zaicrm.com
    ServerAlias *.zaicrm.com
    <Directory /var/www/html/zaicrm.com/>
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
    </Directory>
    ErrorLog logs/zaicrm.com-error_log
    CustomLog logs/zaicrm.com-access_log common
</VirtualHost>

Note: Replace zaicrm.com with your domain name

Once done restart the server using the following command systemctl restart httpd.service

Local Setup(Windows)

Step 1:

Open httpd.conf file present in F:/xampp/apache/conf/httpd.conf ( This folder Remove the #(hash) sign present to include the “httpd-vhosts.conf” file in httpd.conf file.

# Virtual hosts

#Include conf/extra/httpd-vhosts.conf

To

# Virtual hosts

Include conf/extra/httpd-vhosts.conf

Step 2:

Create a virtualhost file. Open “httpd-vhosts.conf” file. And copy the below lines of code.

<VirtualHost *:80>
 DocumentRoot "F:/xampp/htdocs/usersubdomain/"
 ServerName zaiserve.com     
 </VirtualHost>
 <VirtualHost *:80>	
 DocumentRoot "F:/xampp/htdocs/usersubdomain/"
 ServerName systral.zaiserve.com   
 </VirtualHost>
Step3:

Open C:\Windows\System32\drivers\etc\hosts Add the below line at the end of file.

127.0.0.1 zaiserve.com.

127.0.0.1 systral.zaiserve.com.

Restart apache server and visit the site URL.

Note: Wildcard sub domain concept not working under Apache server Virtual host in the local system. We need to setup the sub domain manually using the above steps.

Local Setup(Linux)

Open the terminal

Step 1 — Create the Directory Structure

We’ll be using example domain names, highlighted below. You should replace these with your actual domain names.
  • sudo mkdir -p /var/www/html/example.com/public_html

Step 2 — Grant Permissions

We should now change the permissions to our current non-root user to be able to modify the files.Additionally, we’ll ensure that read access.
  • sudo chown -R $USER:$USER /var/www/html/example.com/public_html

Step 3 — Create Demo Pages for Each Virtual Host

We should now change the permissions to our current non-root user to be able to modify the files. Within this file, create a domain-specific HTML document
  • nano /var/www/html/example.com/public_html/index.html

Step 4 — Create New Virtual Host Files

Apache comes with a default virtual host file called 000-default.conf
  • sudo nano /etc/apache2/sites-available/example.com.conf
Kindly add the following in example.com.conf
<VirtualHost *:80>
    ServerAdmin admin@example.com
    ServerName zaiserve.com
    ServerAlias test.zaiserve.com
    DocumentRoot /var/www/html/zaiserve.com/public_html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Step 5 — Enable the New Virtual Host Files

With our virtual host files created, we must enable them. We’ll be using the a2ensite tool to achieve this goal.
  • sudo a2ensite example.com.conf
Next, disable the default site defined in 000-default.conf:
  • sudo a2dissite 000-default.conf

Note: When you are finished, you need to restart Apache to make these changes take effect and use systemctl status to verify the success of the restart.

  • sudo systemctl restart apache2

Step 6 — Set Up Local Hosts File (Optional)

you can test your work by temporarily modifying the hosts file on your local computer.
  • sudo nano /etc/hosts
Using the domains used in this guide, and replacing your server IP for the your_server_IP text, your file should look like this:

Note: When you are defining the sub-domain need to setup manually.

127.0.0.1   localhost
127.0.1.1   guest-desktop
your_server_IP example.com
your_server_IP test.example.com 

Step 7 — Test your Results

Now that you have your virtual hosts configured, you can test your setup by going to the domains that you configured in your web browser:
  • http://example.com

Application Setup

Application setup & commanda to run

Inital Setup

To set up zaicrm website in your local machine or on any other server follow the steps given below.

1. Open your root folder ( Local machine sample root folder : F:\xampp\htdocs ) in your local machine or server.

2. Open the shell prompt under the root folder. Then execute the following git commands.

git clone https://github.com/balazaigo/zaiservedocs.git

3. After executed above git commands will create the new folder with name zaiserve.

4. Then open the shell prompt under the folder zaiserve and execute the composer update command.

5. Then add .env, .gitignore and .htaccess files under the root folder.

6. Create the database and update the details under .env file for the following items.


 DB_HOST=127.0.0.1
 DB_PORT=3306
 DB_DATABASE=zaiserve
 DB_USERNAME=root
 DB_PASSWORD=

 JWT_TTL = 10080
		

7. Also update the APP_URL=http://localhost/zaiserve under .env file.

8. Then open the shell prompt under the folder zaiserve and execute the php artisan migrate command.

9. Give the write permission(777) to the storage folder under the root of the zaiserve folder.

10. JWT_TTL = 10080 is for setting up login response expiry time for a week. We can customise this based on needs.

Commands to Run

        
composer update
php artisan key:generate
composer require tymon/jwt-auth
php artisan vendor:publish --provider="Tymon\JWTAuth\Providers\LaravelServiceProvider"
php artisan jwt:secret
composer dump-autoload
php artisan migrate
php artisan migrate --path=database/migrations/system/departments
php artisan migrate --path=database/migrations/system/roles
php artisan migrate --path=database/migrations/system/users
php artisan migrate --path=database/migrations/system/menus
php artisan migrate --path=database/migrations/system/locations
php artisan migrate --path=database/migrations/system/teams
php artisan migrate --path=database/migrations/system/settings
php artisan migrate --path=database/migrations/system/sharedlists
php artisan db:seed
php artisan config:cache
php artisan route:cache

Config/Constants

Need to add the following entries under 'providers' array in config/app.php file

        
'providers' => [
    ...		
Tymon\JWTAuth\Providers\LaravelServiceProvider::class,
App\Providers\RoleServiceProvider::class,
App\Providers\UserServiceProvider::class,
App\Providers\UsergroupServiceProvider::class
]

Helpers

Path: app/Helpers/helper.php

1. Generate API reponse - Helper

Use apiResponseMessage helper function when returning response for the user.

function apiResponseMessage($responseArray=[], $responseType='') {
	$responseType = strtolower(trim($responseType));
    $responseArray = [];
    $responseArray = [];
	if($responseType != 'json' || $responseType != 'xml' || $responseType=='' ) {
		$responseType = config('constant.response_type');
	}
    if ($responseType == 'json') {
    	$responseArray = [
    		'message' => $message,
    		'status'  => $code == 200 ? 'success' : 'failure',
    		'code'    => $code,
    		'permissions' => $permissions,
    		'data'    => $data,
    	];
		return response()->json($responseArray, $code);
	} else if ($responseType == 'xml') {
		return response()->xml($responseArray);
	}
 }

2. SanitizeForm - Helper

Use sanitizeFormInputs helper function when adding/updating field values to DB

function sanitizeFormInputs($value = "", $type = "") {
	$response = "";
	if ($value != ""&& $type != "") {
		if ($type == "string") {
			$response = filter_var($value, FILTER_SANITIZE_STRING);
		} else if ($type == "email") {
			$response = filter_var($value, FILTER_SANITIZE_EMAIL);
		} else if ($type == "default") {
			$response = filter_var($value, FILTER_DEFAULT);
		} else if ($type == "number_int") {
			$response = filter_var($value, FILTER_SANITIZE_NUMBER_INT);
		} else {
			$response = filter_var($value, FILTER_UNSAFE_RAW);
		}
	}
	return $response;
 }

3. Store Setting Images - Helper

Use storeSettingImages helper function where to store the setting images.

function storeSettingImages($name, $newFileName, $request) {
    $returnFilename = '';
    $settingFileUploadPath = config('constant.settingFileUploadPath');
    if ($request->hasFile($name)){
        $filename = $newFileName.'.'.$request->$name->getClientOriginalExtension();
        $request->$name->storeAs($settingFileUploadPath, $filename);
        $return_filename = $filename;
    }
    return $return_filename;
}

4. Generate Slug - Helper

Use generateSlug helper function where the slug needs to be used.

function generateSlug($value = "", $delimiter = "-") {
	$response = "";
	if (trim($value) != "") {
		$response = Str::slug(trim($value), '-');
	}
	return $response;
}

5. Generate Status Name - Helper

Use getStatusName helper function where the status name need to be displayed instead of status ID.

function getStatusName($statusId = null){
   	$statusList = config('constant.statusList');
   	$statusName = isset($statusList[$statusId]['name'])?$statusList[$statusId]['name']:null;
  	return $statusName;
 }

6. Generate Subscription Name - Helper

Use getSubscriptionName helper function where the subscription name need to be displayed instead of subscription ID.

function getSubscriptionName($subscriptionId = null){
	$subscriptionList = config('constant.subscription');
	$subscriptionName = isset($subscriptionList[$subscriptionId]['name'])?$subscriptionList[$subscriptionId]
['name']:null; return $subscriptionName; }

7. Encrypt ID - Helper

Use encryptId helper function where to show the ID in encrypted format.

function encryptId($id){
	if ($id != '') {
		return encrypt($id);
	}
 }

8. Decrypt ID - Helper

Use decryptId helper function where to show the ID in encrypted format.

function decryptId($id){
	if ($id != '') {
		return decrypt($id);
	}
 }

9. Date Time Format - Helper

Use dateTimeFormat helper function to format the given date time in needed format.

function dateTimeFormat($date = '') {
	return Carbon::parse($date)->format('Y-m-d H:i:s');
}

10. Date Time Format Subract days- Helper

Use dateTimeFormatSubtractDays helper function to format the given date time with given days of subract in needed format.

function dateTimeFormatSubtractDays($date, $numberOfDays=1) {
	return Carbon::parse($date)->subDays($numberOfDays)->format('Y-m-d');
}

11. Date Format - Helper

Use dateFormat helper function to format the given date in needed format.

function decryptId($date){
	return Carbon::parse($date)->format('Y-m-d');
 }

12. Date Format Include days- Helper

Use dateIncludeDays helper function to format the given date with given days of include in needed format.

function dateIncludeDays($date, $numberOfDays, $format='Y-m-d') {
    return Carbon::parse($date)->addDays($numberOfDays)->format($format);
}

13. Super Admin Logged in - Helper

Use isSuperAdminLoggedIn helper function to check whether the logged in user is Super Admin or not.

function isSuperAdminLoggedIn() {
	$response = '';
	$seconds = 600;
	$response = Cache::remember('isSuperAdminLoggedIn_'.$userId, $seconds, function () use($userId) {
			$userDetails = User::select('id')->where('id',$userId)->whereHas('userRole',  function($query) {
				$query->where('label', config('constant.superAdminRoleLabel')); 
			})->first();
			if(isset($userDetails->id)) {
				return true;
			}
			return false;
		});
	return $response;
}

14. Is Valid User Id - Helper

Use isValidUserId helper function to check whether the given user id is valid or not.

function isValidUserId($userId) {
	if($userId!='') {
		$userDetails = User::find($userId);
		if ($userDetails==null) {
			return false;
		}
		return true;
	}
	return false;
}

15. Handle Exception - Helper

Use handleException helper function wherever handle expection.

function handleException($e, $function, $request = '') {		
	$log = [
		'message'  => $e->getMessage(),
		'file' 	   => $e->getFile(),
		'function' => $function,
		'user' 	   => auth()->user()->name,
	];
	if (!empty($request)) {
		$requestArray = [ 'request'  => $request ];
		$logArray = array_merge($log, $requestArray);
	} else {
		$logArray = $log;
	}
	Log::critical('Error Occurred!', $logArray);
	return ['error_message' => $e->getMessage(), 'status' => 400];
}

16. User Assign Department - Helper

Use userAssignedDepartment helper function to get the department of the logged in user other than super admin.

function userAssignedDepartment($request, $dbQueryObject, $id = 'id') {
	$isSuperAdminLoggedIn = $request->get('isSuperAdminLoggedIn');
	if(!$isSuperAdminLoggedIn) {
		$userId 		= auth()->user()->id;
		$userDepartment = UserDepartment::where('user_id', $userId)->get();
		$departmentId 	= [];
		foreach ($userDepartment as $key => $value) {
			$departmentId[] = $value->department_id;
		}
		if(is_array($departmentId) && count($departmentId) > 0 ) {
			$dbQueryObject = $dbQueryObject->whereIn($id, $departmentId);
		}				
	}

    return $dbQueryObject;
}

17. Store Media - Helper

Use storeMedia helper function to store the given media

function storeMedia($file, $newFileName, $settingFileUploadPath) {
    $returnFileName = '';
    if ($file) {
        $fileName = $newFileName.'.'.$file->getClientOriginalExtension();
        $file->storeAs($settingFileUploadPath, $fileName);
        $returnFileName = $fileName;
    }
    return $returnFileName;
}

18. Middleware Parameters - Helper

Use middlewareParameters helper function to store the given media

function middlewareParameters($includeParam = [], $excludeParam = []) {
	$default = ['api', 'jwt.verify'];
	if (!empty($includeParam)) {
		$default = array_merge($default, $includeParam);
	}
	if (($key = array_search($excludeParam, $default)) !== false) {
	    unset($default[$key]);
	}
	return $default;
}

19. Date Period - Helper

Use datePeriod helper function to get the date periods with start date and end date.

function datePeriod($joiningDate, $reliveDate) {
	$period 	= 3;
	$format 	= 'd-m-Y';
	$reliveDate = $reliveDate == null ? dateFormat() : $reliveDate;
	$datetime1  = new \DateTime($joiningDate);
	$datetime2  = new \DateTime($reliveDate);
	$interval   = $datetime1->diff($datetime2);
	$days 		= $interval->format('%a');//now do whatever you like with $days
	$a 		 = round($days/$period);
	$newDate = $datetime1;
	for($i = 1; $i <= $period; $i++){
		$newDate = date_add($newDate, date_interval_create_from_date_string($a." days"));
		$datePeriod[] = $newDate->format($format);
	}
	return $datePeriod;
}

20. Set Status Value - Helper

Use setStatusValue helper function to set status value based on given input for status.

function setStatusValue($status = null){
   $statusValue = strtolower($status) == 'active' ? 5 : 4; 
   return $statusValue;
}

21. Get User Type - Helper

Use getUserType helper function to get whether user type is Admin or Employee.

function getUserType($userTypeId = null) {
    $userTypeList = config('constant.userTypeList');
    $userTypeName = isset($userTypeList[$userTypeId]['name'])?$userTypeList[$userTypeId]['name']:null;
    return $userTypeName;
}

22. Get List Type - Helper

Use getListType helper function to get list type on shared lists.

function getListType($listTypeID = null) {
    $listType = config('constant.listType');
    $listTypeName = isset($listType[$listTypeID]['name'])?$listType[$listTypeID]['name']:null;
    return $listTypeName;
}

23. Decrypt Array - Helper

Use decryptArray helper function to decrypt array values.

function decryptArray($decryptIdsArray = null) {
	if( count($decryptIdsArray) > 0 ) {
		return array_map(function($el) { return decryptId($el); }, $decryptIdsArray);	
    }
}

24. Password Validation - Helper

Use getPasswordValidation helper function get validation regex for password validate.

function getPasswordValidation($passwordId = null) {
	$passwordValidationList = config('constant.passwordValidation');
	$passwordValidationName = isset($passwordValidationList[$passwordId]['name'])?$passwordValidationList[$passwordId]['name']:null;
	return $passwordValidationName;
}

25. Validate Error Obj - Helper

Use validateErrorObj helper function to changes validation array message to object validation message.

function validateErrorObj($errors) {
    $transformed = [];
    foreach ($errors as $field => $messages) {
        $transformed[$field] = $messages[0];
    }
    return $transformed;
}

26. Generate Pagination - Helper

Use apiGenerateListData helper function to generate pagination.

function apiGenerateListData($resultData) {
	$perPage = config('constant.pagination');
	//This array contain all data to be sent to the view
	$data = array();
	//Get current page from url like ?page=6  --- resolveCurrentPage --> gets the value 6.
	$currentPage        = LengthAwarePaginator::resolveCurrentPage();
	//Create a new collection from the array data
	$collection         = new Collection($resultData);
	//Define how many items we want to be visible in each page
	$per_page           = $perPage;
	//Slice the collection to get the items to display in current page
	$currentPageResults = $collection->slice(($currentPage-1) * $per_page, $per_page)->all();
	//Create our paginator and add it to the data array
	$data['result']     = new LengthAwarePaginator($currentPageResults, count($collection), $per_page);
	//Set base url for pagination links to follow e.g custom/url?page=6
	$data['result']->setPath(Request::url());

	return $data;
}

Path: app/Helpers/permission.php

1. Get Menu Item Action Permission Current - Helper

Usage: Use getMenuItemActionPermissionCurrent helper function to get menu item and actions permission for the logged-in user.

Parameters: getMenuItemActionPermissionCurrent($userId, $routeName, $routeMenuItemSlugMapping,$descendantRoleUsers)

2. Get Permission Det - Helper

Usage: Use getpermissionDet helper function to get permission details for the logged-in user.

Parameters: getpermissionDet($userId, $userActiveRoles, $menuItemSlugCore, $menuActionSlug, $menuItemsDep)

3. Get Active Roles User - Helper

Usage: Use getActiveRolesUser helper function to get active role for the logged-in user.

Parameters: getActiveRolesUser($userId)

4. Get Permission Details - Helper

Usage: Use getPermissionDetails helper function to get permission details of the requested URL.

Parameters: getPermissionDetails($request)

5. Restrict Records - Helper

Usage: Use restrictRecords helper function to restrict the records based on logged-in user.

Parameters: restrictRecords($request, $dbQueryObject, $createdByField='created_by')

6. Validate Department Ids - Helper

Usage: Use validateDepartmentIds helper function to validate given departments.

Parameters: validateDepartmentIds($departmentIds)

7. Validate Role Ids - Helper

Usage: Use validateRoleIds helper function to validate given roles.

Parameters: validateRoleIds($roleIds)

8. Validate Team Ids - Helper

Usage: Use validateTeamIds helper function to validate given teams.

Parameters: validateTeamIds($teamIds)

9. Validate Menu Group Ids - Helper

Usage: Use validateMenuGroupIds helper function to validate given menu groups.

Parameters: validateMenuGroupIds($menuGroupIds)

10. Validate Menu Item Ids - Helper

Usage: Use validateMenuItemId helper function to validate given menu items.

Parameters: validateMenuItemId($menuItemId)

11. Validate Department Team Mapping - Helper

Usage: Use validateDeptTeamMappings helper function to validate given department and team.

Parameters: validateDeptTeamMappings($deptTeamIds)

12. Validate Department Role Mapping - Helper

Usage: Use validateDeptRoleMappings helper function to validate given department and role.

Parameters: validateDeptRoleMappings($depRoleIds)

13. Get Setting Value - Helper

Usage: Use getSettingValue helper function to get setting value.

Parameters: getSettingValue($variableNameArr)

14. Add Default Role Permissions - Helper

Usage: Use addDefaultRolePermissions helper function to add default role permissions for the role.

Parameters: addDefaultRolePermissions($roleDetails)

Packages

Custom function

class BaseModel extends \Eloquent
{
    public static function boot()
	{
	    parent::boot();

	    static::creating(function($model)
	    {
	        $model->created_by = auth()->user()->id;
	    });

	    static::updating(function($model)
	    {
	        $model->updated_by = auth()->user()->id;
	    });
	}
}

Note: While create a model for CRUD include 'use App\Model\BaseModel;' instead of 'use Illuminate\Database\Eloquent\Model;' extend the 'BaseModel' instead of extending 'Model'

User Management

Role Management

Department Management

Menu Management

Location Management

Team Management

Shared Group Management

item-6-2