Skip to content

Commit

Permalink
allow partial customerdb.read based on last_changed date
Browse files Browse the repository at this point in the history
  • Loading branch information
schorschii committed Nov 1, 2022
1 parent 657510d commit 0f8505c
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 39 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ With this PHP web app you can set up your own server for the [Android](https://g
0. Install Apache 2, PHP 7 (with `php-curl`) and MySQL/MariaDB on a Linux server.
1. Download the [latest release](https://github.com/schorschii/customerdb-server/releases) and unpack it into `/srv/www/customerdb`.
2. Set your Apache (virtual host) web root directory to `/srv/www/customerdb/web` by editing the corresponding configuration file inside `/etc/apache2/sites-enabled`.
3. Create a database on your MySQL server and import the schema from `lib/sql/customerdb.sql`.
3. Create a database on your MySQL server and import the schema from `sql/customerdb.sql`.
4. Edit/create `conf.php` from the example file (`conf.php.example`) and enter your MySQL credentials. Please do not use the root user but create a new user which is only allowed to operate on the specific database.
5. Select "Own Server" in the settings of your Customer Database app and enter the full URL to the `web/api.php` script. Example: `http://192.168.2.10/api.php`.
6. Create an account. You can do this in the app (if the API and registration is enabled in `conf.php`) or by using the command line tool on the server (`php console.php createuser <username> <password>`).

## Installation (On A Managed Server)
1. Download the [latest release](https://github.com/schorschii/customerdb-server/releases) and unpack it into your webspace.
2. Create a database on your MySQL server and import the schema from `lib/sql/customerdb.sql` using phpMyAdmin or a similar web-based tool from your hosting provider.
2. Create a database on your MySQL server and import the schema from `sql/customerdb.sql` using phpMyAdmin or a similar web-based tool from your hosting provider.
3. Edit/create `conf.php` from the example file (`conf.php.example`) and enter your MySQL credentials.
4. Select "Own Server" in the settings of your Customer Database app and enter the full URL to the `web/api.php` script. Example: `http://example.com/web/api.php`.
5. Ensure that the API and registration is enabled in `conf.php`. Now create a sync account inside the app.
Expand Down
6 changes: 4 additions & 2 deletions docs/JSON REST API.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ A valid JSON-RPC request is sent via HTTP with the HTTP header `Content-Type: ap
```

# Methods
## `customers.read` - get all customers from server
## `customers.read` - get customers from server
### Parameters
- `diff_since` (optional) - only return records changed since this date
- `username` - username for authentication
- `password` - password for authentication
- `playstore_token` (optional) - Google Play Store token for server-side subscription payment check (not of interest for self-hosted servers)
Expand All @@ -31,6 +32,7 @@ A valid JSON-RPC request is sent via HTTP with the HTTP header `Content-Type: ap
"id": 1,
"method": "customerdb.read",
"params": {
"diff_since": "2022-11-01 20:48:00",
"username": "[email protected]",
"password": "12345678"
}
Expand Down Expand Up @@ -75,7 +77,7 @@ A valid JSON-RPC request is sent via HTTP with the HTTP header `Content-Type: ap
}
```

## `customers.put` - upload/update all customers to server
## `customers.put` - update/create customers on server
### Parameters
- `username` - username for authentication
- `password` - password for authentication
Expand Down
8 changes: 6 additions & 2 deletions docs/Upgrade.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
# Upgrade Instructions

## v1.3
- upgrade database schema as defined in `lib/customerdb.sql`
- add column 'last_modified_on_server' to Customer, Voucher, Calendar, Appointment and Setting table

## v1.2
- upgrade database schema as defined in lib/customerdb.sql
- upgrade database schema as defined in `lib/customerdb.sql`
- add column 'customer_id' to Appointment table
- add column 'from_customer_id' and 'for_customer_id' to Voucher table
- replace all old files except conf.php

## v1.1
- upgrade database schema as defined in lib/customerdb.sql
- upgrade database schema as defined in `lib/customerdb.sql`
- add table Appointment
- add table Calendar
- add new table's indices and constraints
Expand Down
18 changes: 18 additions & 0 deletions docs/decisions/0001-UTC Timestamps.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Agent-Server Communication Method
Architecture Decision Record
Lang: en
Encoding: utf-8
Date: 2022-11-01
Author: Georg Sieber

## Decision
All timestamps are stored in UTC time in the database on the server.

## Status
Accepted

## Context
Since the Customer Database app targets users all around the world, timestamps must be in UTC time to avoid time zone conflicts.

## Consequences
All communication over the API must also use UTC timestamps.
18 changes: 14 additions & 4 deletions lib/api-data.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,18 @@ function handleApiRequestData($srcdata) {
switch($srcdata['method']) {

case 'customerdb.read':
$customers = $db->getCustomersByClient($userId);
$vouchers = $db->getVouchersByClient($userId);
$calendars = $db->getCalendarsByClient($userId);
$appointments = $db->getAppointmentsByClient($userId);
$diffSince = null;
if(isset($srcdata['params']['diff_since'])) $diffSince = strtotime($srcdata['params']['diff_since']);
if($diffSince === false) { // strtotime() returns false in case of parsing error
$resdata['result'] = null;
$resdata['error'] = 'Invalid Date';
break;
}

$customers = $db->getCustomersByClient($userId, date('Y-m-d H:i:s', $diffSince));
$vouchers = $db->getVouchersByClient($userId, date('Y-m-d H:i:s', $diffSince));
$calendars = $db->getCalendarsByClient($userId, date('Y-m-d H:i:s', $diffSince));
$appointments = $db->getAppointmentsByClient($userId, date('Y-m-d H:i:s', $diffSince));
foreach($customers as $customer) {
if($customer->image != null)
$customer->image = base64_encode($customer->image);
Expand All @@ -93,13 +101,15 @@ function handleApiRequestData($srcdata) {
'calendars' => $calendars,
'appointments' => $appointments,
];
#error_log(count($customers).' customers changed since '.date('Y-m-d H:i:s',$diffSince)); // debug
break;

case 'customerdb.put':
$success = true;
$db->getDbHandle()->beginTransaction();

// todo check if all attr delivered before accessing it in array
#error_log(count($srcdata['params']['customers']).' customers put'); // debug
foreach($srcdata['params']['customers'] as $customer) {
foreach(['id', 'title', 'first_name', 'last_name', 'phone_home', 'phone_work',
'email', 'street', 'zipcode', 'city', 'country', 'customer_group',
Expand Down
Loading

0 comments on commit 0f8505c

Please sign in to comment.