addresses: enable POSTing Postal Code entities (back + front)

This commit is contained in:
nobohan 2021-06-11 14:33:22 +02:00
parent 5daf5cbe84
commit 1b709d39a4
13 changed files with 247 additions and 39 deletions

View File

@ -321,7 +321,8 @@ class ChillMainExtension extends Extension implements PrependExtensionInterface,
'_entity' => [
'methods' => [
Request::METHOD_GET => true,
Request::METHOD_HEAD => true
Request::METHOD_HEAD => true,
Request::METHOD_POST => true,
]
],
]

View File

@ -26,7 +26,7 @@ class PostalCode
* @ORM\Id
* @ORM\Column(name="id", type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
* @groups({"read"})
* @groups({"write", "read"})
*/
private $id;
@ -34,7 +34,7 @@ class PostalCode
* @var string
*
* @ORM\Column(type="string", length=255, name="label")
* @groups({"read"})
* @groups({"write", "read"})
*/
private $name;
@ -42,7 +42,7 @@ class PostalCode
* @var string
*
* @ORM\Column(type="string", length=100)
* @groups({"read"})
* @groups({"write", "read"})
*/
private $code;
@ -50,7 +50,7 @@ class PostalCode
* @var Country
*
* @ORM\ManyToOne(targetEntity="Chill\MainBundle\Entity\Country")
* @groups({"read"})
* @groups({"write", "read"})
*/
private $country;

View File

@ -49,7 +49,19 @@ export default {
};
if (address.selected.address.point !== undefined){
newAddress = Object.assign(newAddress, {'point': address.selected.address.point.coordinates});
createdAddress = Object.assign(createdAddress, {
'point': address.selected.address.point.coordinates
});
}
if(address.writeNewPostalCode){
let newPostalCode = address.newPostalCode;
newPostalCode = Object.assign(newPostalCode, {
'country': {'id': address.selected.country.id },
});
createdAddress = Object.assign(createdAddress, {
'newPostalCode': newPostalCode
});
}
this.$store.dispatch('addAddress', newAddress);

View File

@ -16,7 +16,10 @@ const addressMessages = {
flat: 'Appartement',
buildingName: 'Nom du batiment',
extra: 'Complément d\'adresse',
distribution: 'Service particulier de distribution'
distribution: 'Service particulier de distribution',
create_postal_code: 'Localité inconnue. Cliquez ici pour créer une nouvelle localité',
postalCode_name: 'Nom de la localité',
postalCode_code: 'Code postal de la localité'
}
};

View File

@ -1,7 +1,7 @@
import 'es6-promise/auto';
import { createStore } from 'vuex';
import { postAddress } from '../../_api/AddAddress'
import { postAddress, postPostalCode } from '../../_api/AddAddress'
const debug = process.env.NODE_ENV !== 'production';
@ -25,19 +25,32 @@ const store = createStore({
actions: {
addAddress({ commit }, payload) {
console.log('@A addAddress payload', payload);
//commit('addAddress', payload); // à remplacer par la suite
//fetch POST qui envoie l'adresse, et récupère la confirmation que c'est ok.
//La confirmation est l'adresse elle-même.
if('newPostalCode' in payload){
postPostalCode(payload.newPostalCode)
.then(postalCode => {
let body = payload;
body.postcode = {'id': postalCode.id },
postAddress(body)
.then(address => new Promise((resolve, reject) => {
commit('addAddress', address);
resolve();
}))
.catch((error) => {
commit('catchError', error);
});
})
postAddress(payload)
.then(address => new Promise((resolve, reject) => {
commit('addAddress', address);
resolve();
}))
.catch((error) => {
commit('catchError', error);
});
} else {
postAddress(payload)
.then(address => new Promise((resolve, reject) => {
commit('addAddress', address);
resolve();
}))
.catch((error) => {
commit('catchError', error);
});
}
}
}
});

View File

@ -66,7 +66,6 @@ const fetchAddresses = () => {
* @returns {Promise}
*/
const postAddress = (address) => {
console.log(address);
const url = `/api/1.0/main/address.json?`;
const body = address;
@ -91,7 +90,6 @@ const postAddress = (address) => {
* @body Object - dictionary with changes to post
*/
const patchAddress = (id, body) => {
const url = `/api/1.0/main/address/${id}.json`;
return fetch(url, {
method: 'PATCH',
@ -106,7 +104,26 @@ const patchAddress = (id, body) => {
});
};
/*
* Endpoint chill_api_single_postal_code__entity_create
* method POST, post Postal Code Object
* @returns {Promise}
*/
const postPostalCode = (postalCode) => {
const url = `/api/1.0/main/postal-code.json?`;
const body = postalCode;
return fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json;charset=utf-8'
},
body: JSON.stringify(body)
}).then(response => {
if (response.ok) { return response.json(); }
throw Error('Error with request resource response');
});
};
export {
fetchCountries,
@ -114,5 +131,6 @@ export {
fetchReferenceAddresses,
fetchAddresses,
postAddress,
patchAddress
patchAddress,
postPostalCode
};

View File

@ -106,6 +106,7 @@ export default {
},
address: {
writeNewAddress: false,
writeNewPostalCode: false,
loaded: {
countries: [],
cities: [],
@ -116,6 +117,10 @@ export default {
city: {},
address: {},
},
newPostalCode: {
code: null,
name: null
},
addressMap: {
center : [48.8589, 2.3469], // Note: LeafletJs demands [lat, lon] cfr https://macwright.com/lonlat/
zoom: 12
@ -211,6 +216,8 @@ export default {
this.address.distribution = null;
this.address.extra = null;
this.address.writeNewAddress = false;
this.address.writeNewPostalCode = false;
this.address.newPostalCode = {};
console.log('cities and addresses', this.address.loaded.cities, this.address.loaded.addresses);
}
}

View File

@ -15,7 +15,7 @@
:options="addresses">
</VueMultiselect>
</div>
<div v-if="writeNewAddress">
<div v-if="writeNewAddress || writeNewPostalCode">
<input
type="text"
name="street"
@ -45,6 +45,9 @@ export default {
writeNewAddress() {
return this.address.writeNewAddress;
},
writeNewPostalCode() {
return this.address.writeNewPostalCode;
},
addresses() {
return this.address.loaded.addresses;
},

View File

@ -8,9 +8,25 @@
label="value"
:custom-label="transName"
:placeholder="$t('select_city')"
:taggable="true"
:multiple="false"
@tag="addPostalCode"
:tagPlaceholder="$t('create_postal_code')"
:options="cities">
</VueMultiselect>
</div>
<div v-if="writeNewPostalCode">
<input
type="text"
name="name"
:placeholder="$t('postalCode_name')"
v-model="name"/>
<input
type="text"
name="code"
:placeholder="$t('postalCode_code')"
v-model="code"/>
</div>
</template>
<script>
@ -25,6 +41,30 @@ export default {
value: null
}
},
computed: {
writeNewPostalCode() {
return this.address.writeNewPostalCode;
},
cities() {
return this.address.loaded.cities;
},
name: {
set(value) {
this.address.newPostalCode.name = value;
},
get() {
return this.address.newPostalCode.name;
}
},
code: {
set(value) {
this.address.newPostalCode.code= value;
},
get() {
return this.address.newPostalCode.code;
}
},
},
methods: {
transName(value) {
return `${value.code}-${value.name}`
@ -33,10 +73,8 @@ export default {
this.address.selected.city = value;
this.getReferenceAddresses(value);
},
},
computed: {
cities() {
return this.address.loaded.cities;
addPostalCode() {
this.address.writeNewPostalCode = true;
}
}
};

View File

@ -28,7 +28,7 @@ export default {
methods: {
init() {
if (this.value !== undefined) {
this.getCities(this.value);
this.selectCountry(this.value);
}
},
transName ({ name }) {

View File

@ -200,6 +200,60 @@ paths:
description: "Unprocessable entity (validation errors)"
400:
description: "transition cannot be applyed"
patch:
tags:
- address
summary: patch an address
requestBody:
required: true
content:
application/json:
schema:
type: object
properties:
buildingName:
type: string
corridor:
type: string
distribution:
type: string
extra:
type: string
flat:
type: string
floor:
type: string
isNoAddress:
type: boolean
point:
type: array
items:
type: number
minItems: 2
maxItems: 2
postcode:
$ref: '#/components/schemas/PostalCode'
steps:
type: string
street:
type: string
streetNumber:
type: string
validFrom:
type: string
validTo:
type: string
responses:
401:
description: "Unauthorized"
404:
description: "Not found"
200:
description: "OK"
422:
description: "Unprocessable entity (validation errors)"
400:
description: "transition cannot be applyed"
/1.0/main/address/{id}.json:
get:
@ -283,6 +337,35 @@ paths:
responses:
200:
description: "ok"
post:
tags:
- address
summary: create a new PostalCode
requestBody:
required: true
content:
application/json:
schema:
type: object
properties:
name:
type: string
code:
type: string
country:
$ref: '#/components/schemas/Country'
responses:
401:
description: "Unauthorized"
404:
description: "Not found"
200:
description: "OK"
422:
description: "Unprocessable entity (validation errors)"
400:
description: "transition cannot be applyed"
/1.0/main/postal-code/{id}.json:
get:
tags:

View File

@ -85,7 +85,19 @@ export default {
};
if (address.selected.address.point !== undefined){
createdAddress = Object.assign(createdAddress, {'point': address.selected.address.point.coordinates});
createdAddress = Object.assign(createdAddress, {
'point': address.selected.address.point.coordinates
});
}
if(address.writeNewPostalCode){
let newPostalCode = address.newPostalCode;
newPostalCode = Object.assign(newPostalCode, {
'country': {'id': address.selected.country.id },
});
createdAddress = Object.assign(createdAddress, {
'newPostalCode': newPostalCode
});
}
this.$store.dispatch('addAddress', createdAddress);

View File

@ -1,9 +1,8 @@
import 'es6-promise/auto';
import { createStore } from 'vuex';
import { postAddress } from 'ChillMainAssets/vuejs/_api/AddAddress';
import { postAddress, postPostalCode, patchAddress } from 'ChillMainAssets/vuejs/_api/AddAddress';
import { postAddressToHousehold } from '../api';
import { patchAddress } from '../../../../../../ChillMainBundle/Resources/public/vuejs/_api/AddAddress';
const debug = process.env.NODE_ENV !== 'production';
@ -38,14 +37,33 @@ const store = createStore({
addAddress({ commit }, payload) {
console.log('@A addAddress payload', payload);
postAddress(payload)
.then(address => new Promise((resolve, reject) => {
commit('addAddress', address);
resolve();
}))
.catch((error) => {
commit('catchError', error);
});
if('newPostalCode' in payload){
postPostalCode(payload.newPostalCode)
.then(postalCode => {
console.log(postalCode);
let body = payload;
body.postcode = {'id': postalCode.id },
console.log(body);
postAddress(body)
.then(address => new Promise((resolve, reject) => {
commit('addAddress', address);
resolve();
}))
.catch((error) => {
commit('catchError', error);
});
})
} else {
postAddress(payload)
.then(address => new Promise((resolve, reject) => {
commit('addAddress', address);
resolve();
}))
.catch((error) => {
commit('catchError', error);
});
}
},
addDateToAddressAndAddressToHousehold({ commit }, payload) {
console.log('@A addDateToAddressAndAddressToHousehold payload', payload);