Skip to content

Commit 911c096

Browse files
committed
feat: add new API
1 parent dfb9d52 commit 911c096

File tree

18 files changed

+474
-493
lines changed

18 files changed

+474
-493
lines changed

.DS_Store

0 Bytes
Binary file not shown.

.editorconfig

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# editorconfig.org
2+
root = true
3+
4+
[*]
5+
indent_style = space
6+
indent_size = 4
7+
end_of_line = lf
8+
charset = utf-8
9+
trim_trailing_whitespace = true
10+
insert_final_newline = true
11+
12+
[*.md]
13+
trim_trailing_whitespace = false

controllers/front/graphql.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,16 @@ public function setMedia()
2626
parent::setMedia();
2727
$this->context->controller->registerStylesheet(
2828
'modules-vuefront-front-css',
29+
'modules/vuefront/views/css/graphiql.css',
2930
'modules/vuefront/views/css/index.css',
3031
array('media' => 'all', 'priority' => 200)
3132
);
3233

3334
$this->context->controller->registerJavascript(
3435
'modules-vuefront-front-js',
35-
'modules/vuefront/views/js/middleware.js',
36+
'modules/vuefront/views/js/react.production.min.js',
37+
'modules/vuefront/views/js/react-dom.production.min.js',
38+
'modules/vuefront/views/js/graphiql.js',
3639
array('position' => 'head', 'priority' => 0)
3740
);
3841
}

mapping.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"home": "common/home/get",
2424

2525
"updateApp": "common/home/updateApp",
26+
"updateSite": "common/home/updateSite",
2627

2728
"searchUrl": "common/home/searchUrl",
2829

@@ -57,6 +58,7 @@
5758
"addToCart": "store/cart/add",
5859
"updateCart": "store/cart/update",
5960
"removeCart": "store/cart/remove",
61+
"clearCart": "store/cart/clear",
6062

6163
"category": "store/category/get",
6264
"categoriesList": "store/category/getList",
@@ -83,5 +85,7 @@
8385
"removeWishlist": "store/wishlist/remove",
8486

8587
"contactSend": "common/contact/send",
86-
"contact": "common/contact/get"
88+
"contact": "common/contact/get",
89+
90+
"version": "common/home/version"
8791
}

model/common/customer.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,36 @@
1212

1313
class ModelCommonCustomer extends Model
1414
{
15+
public function updateCustomerData($id, $data)
16+
{
17+
$sql = new DbQuery();
18+
$sql->select('*');
19+
$sql->from('vuefront_customer', 'v');
20+
$sql->where('id_customer = \''.$id.'\'');
21+
22+
$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql);
23+
if (!$result) {
24+
Db::getInstance(_PS_USE_SQL_SLAVE_)->execute('INSERT INTO `' . _DB_PREFIX_ . 'vuefront_customer` SET id_customer = \''.$id.'\', phone = \''.$data['phone'].'\'');
25+
} else {
26+
DB::getInstance(_PS_USE_SQL_SLAVE_)->execute('UPDATE `'._DB_PREFIX_.'vuefront_customer` SET phone = \''.$data['phone'].'\' WHERE id_customer = \''.$id.'\'');
27+
}
28+
}
29+
public function getCustomerData($id)
30+
{
31+
$sql = new DbQuery();
32+
$sql->select('*');
33+
$sql->from('vuefront_customer', 'v');
34+
$sql->where('id_customer = \''.$id.'\'');
35+
$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql);
36+
37+
if (!$result || count($result) === 0) {
38+
return [
39+
'phone' => ''
40+
];
41+
}
42+
43+
return $result[0];
44+
}
1545
public function getCustomers($data = array())
1646
{
1747
$sort = '';

model/common/vuefront.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ public function editApp($name, $appSetting)
4242

4343
public function checkAccess()
4444
{
45-
4645
$option = Configuration::get('vuefront-settings');
4746

4847
$setting = array();

resolver/common/account.php

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,13 @@ public function getCustomer($args)
5656
{
5757
$this->load->model('common/customer');
5858
$customer_info = $this->get($args['id']);
59+
5960
return array(
6061
'id' => $customer_info['id'],
6162
'firstName' => $customer_info['firstName'],
6263
'lastName' => $customer_info['lastName'],
6364
'email' => $customer_info['email'],
65+
'phone' => $customer_info['phone']
6466
);
6567
}
6668

@@ -135,15 +137,21 @@ public function register($args)
135137
$customer->firstname = $customerData['firstName'];
136138
$customer->lastname = $customerData['lastName'];
137139
$customer->email = $customerData['email'];
138-
140+
139141
if (_PS_VERSION_ > '1.7.0.0') {
140142
$crypto = ServiceLocator::get('\\PrestaShop\\PrestaShop\\Core\\Crypto\\Hashing');
141143
$customer->passwd = $crypto->hash($customerData['password']);
142144
} else {
143145
$customer->passwd = Tools::encrypt($customerData['password']);
144146
}
145-
147+
146148
$customer->save();
149+
150+
$this->load->model('common/customer');
151+
152+
$this->model_common_customer->updateCustomerData($customer->id, [
153+
'phone' => $args['phone']
154+
]);
147155
$this->load->model('common/vuefront');
148156
$this->model_common_vuefront->pushEvent('create_customer', array(
149157
'customer_id' => $customer->id,
@@ -162,6 +170,12 @@ public function edit($args)
162170
$this->context->customer->firstname = $customerData['firstName'];
163171
$this->context->customer->lastname = $customerData['lastName'];
164172

173+
$this->load->model('common/customer');
174+
175+
$this->model_common_customer->updateCustomerData($this->context->cookie->id_customer, [
176+
'phone' => $customerData['phone']
177+
]);
178+
165179
if (!$this->context->customer->save()) {
166180
throw new Exception("Update failed");
167181
}
@@ -190,18 +204,23 @@ public function get($user_id)
190204
{
191205
$customer = new Customer($user_id);
192206

207+
$this->load->model('common/customer');
208+
209+
$customerData = $this->model_common_customer->getCustomerData($customer->id);
210+
193211
return array(
194212
'id' => $customer->id,
195213
'email' => $customer->email,
196214
'firstName' => $customer->firstname,
197-
'lastName' => $customer->lastname
215+
'lastName' => $customer->lastname,
216+
'phone' => $customerData['phone']
198217
);
199218
}
200219

201220
public function isLogged()
202221
{
203222
$customer = array();
204-
223+
205224
if ($this->context->cookie->isLogged()) {
206225
$customer = $this->get($this->context->cookie->id_customer);
207226
}
@@ -241,7 +260,7 @@ public function address($args)
241260
public function addressList()
242261
{
243262
$address = array();
244-
263+
245264
$result = $this->context->customer->getAddresses($this->context->cookie->id_lang);
246265

247266
foreach ($result as $value) {

resolver/common/home.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,49 @@ public function updateApp($args)
4343
return $this->model_common_vuefront->getApp($args['name']);
4444
}
4545

46+
public function updateSite($args)
47+
{
48+
try {
49+
$tmpFile = tempnam(sys_get_temp_dir(), 'TMP_');
50+
rename($tmpFile, $tmpFile .= '.tar');
51+
$ch = curl_init();
52+
53+
curl_setopt($ch, CURLOPT_HEADER, 0);
54+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
55+
curl_setopt($ch, CURLOPT_URL, "https://vuefront2019.s3.amazonaws.com/sites/".$args['number']."/vuefront-app.tar");
56+
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 150);
57+
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
58+
59+
$data = curl_exec($ch);
60+
curl_close($ch);
61+
file_put_contents($tmpFile, $data);
62+
$this->removeDir(_PS_ROOT_DIR_ . '/vuefront');
63+
$phar = new PharData($tmpFile);
64+
$phar->extractTo(_PS_ROOT_DIR_ . '/vuefront');
65+
return true;
66+
} catch (\Exception $e) {
67+
echo $e->getMessage();
68+
}
69+
return false;
70+
}
71+
72+
private function removeDir($dir)
73+
{
74+
if (is_dir($dir)) {
75+
$objects = scandir($dir);
76+
foreach ($objects as $object) {
77+
if ($object != "." && $object != "..") {
78+
if (is_dir($dir . "/" . $object) && !is_link($dir . "/" . $object)) {
79+
$this->removeDir($dir . "/" . $object);
80+
} else {
81+
unlink($dir . "/" . $object);
82+
}
83+
}
84+
}
85+
rmdir($dir);
86+
}
87+
}
88+
4689
public function authProxy($args)
4790
{
4891
$this->load->model('common/vuefront');
@@ -63,4 +106,9 @@ public function authProxy($args)
63106

64107
return $result['token'];
65108
}
109+
110+
public function version($args)
111+
{
112+
return '1.0.0';
113+
}
66114
}

resolver/startup/startup.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ class ResolverStartupStartup extends Resolver
1717
{
1818
public function index()
1919
{
20-
2120
if (Tools::getValue('cors')) {
2221
if (!empty($_SERVER['HTTP_ORIGIN'])) {
2322
header('Access-Control-Allow-Origin: ' . $_SERVER['HTTP_ORIGIN']);
@@ -26,7 +25,7 @@ public function index()
2625
}
2726
header('Access-Control-Allow-Methods: POST, OPTIONS');
2827
header('Access-Control-Allow-Credentials: true');
29-
header('Access-Control-Allow-Headers: DNT,User-Agent,X-Requested-With, ' .
28+
header('Access-Control-Allow-Headers: Authorization, DNT,User-Agent,X-Requested-With, ' .
3029
'If-Modified-Since,Cache-Control,Content-Type,Range,Token,token,Cookie,cookie,content-type');
3130
}
3231

resolver/store/cart.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,4 +267,12 @@ public static function getIdProductAttributeByIdAttributes($idProduct, $idAttrib
267267

268268
return $idProductAttribute;
269269
}
270+
public function clear($args)
271+
{
272+
$products = $this->context->cart->getProducts();
273+
foreach ($products as $product) {
274+
$this->context->cart->deleteProduct($product["id_product"]);
275+
}
276+
return $this->get($args);
277+
}
270278
}

schema.graphql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,14 @@ type Customer {
9191
firstName: String
9292
lastName: String
9393
email: String
94+
phone: String
9495
}
9596

9697
input CustomerInput {
9798
firstName: String
9899
lastName: String
99100
email: String
101+
phone: String
100102
password: String
101103
}
102104

@@ -484,6 +486,7 @@ type RootQueryType {
484486
manufacturerList(page: Int = 1, size: Int = 10, search: String = "", sort: String = "name", order: String = "ASC"): ManufacturerResult
485487
manufacturer(id: String): Manufacturer
486488
authProxy(app: String): String
489+
version: String
487490
}
488491

489492
type RootMutationType {
@@ -500,6 +503,7 @@ type RootMutationType {
500503
addToCart(id: String, quantity: Int = 1, options: [CartOption] = []): Cart
501504
updateCart(key: String, quantity: Int = 1): Cart
502505
removeCart(key: String): Cart
506+
clearCart: Cart
503507
addToCompare(id: Int): [Product]
504508
removeCompare(id: String): [Product]
505509
editCurrency(code: String): [Currency]

schemaAdmin.graphql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,5 @@ type RootQueryType {
4848
}
4949
type RootMutationType {
5050
updateApp(name: String, settings: InputAppSetting): AppSetting
51+
updateSite(number: Int): Boolean
5152
}

0 commit comments

Comments
 (0)