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' => [ '_entity' => [
'methods' => [ 'methods' => [
Request::METHOD_GET => true, 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\Id
* @ORM\Column(name="id", type="integer") * @ORM\Column(name="id", type="integer")
* @ORM\GeneratedValue(strategy="AUTO") * @ORM\GeneratedValue(strategy="AUTO")
* @groups({"read"}) * @groups({"write", "read"})
*/ */
private $id; private $id;
@ -34,7 +34,7 @@ class PostalCode
* @var string * @var string
* *
* @ORM\Column(type="string", length=255, name="label") * @ORM\Column(type="string", length=255, name="label")
* @groups({"read"}) * @groups({"write", "read"})
*/ */
private $name; private $name;
@ -42,7 +42,7 @@ class PostalCode
* @var string * @var string
* *
* @ORM\Column(type="string", length=100) * @ORM\Column(type="string", length=100)
* @groups({"read"}) * @groups({"write", "read"})
*/ */
private $code; private $code;
@ -50,7 +50,7 @@ class PostalCode
* @var Country * @var Country
* *
* @ORM\ManyToOne(targetEntity="Chill\MainBundle\Entity\Country") * @ORM\ManyToOne(targetEntity="Chill\MainBundle\Entity\Country")
* @groups({"read"}) * @groups({"write", "read"})
*/ */
private $country; private $country;

View File

@ -49,7 +49,19 @@ export default {
}; };
if (address.selected.address.point !== undefined){ 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); this.$store.dispatch('addAddress', newAddress);

View File

@ -16,7 +16,10 @@ const addressMessages = {
flat: 'Appartement', flat: 'Appartement',
buildingName: 'Nom du batiment', buildingName: 'Nom du batiment',
extra: 'Complément d\'adresse', 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 'es6-promise/auto';
import { createStore } from 'vuex'; import { createStore } from 'vuex';
import { postAddress } from '../../_api/AddAddress' import { postAddress, postPostalCode } from '../../_api/AddAddress'
const debug = process.env.NODE_ENV !== 'production'; const debug = process.env.NODE_ENV !== 'production';
@ -25,19 +25,32 @@ const store = createStore({
actions: { actions: {
addAddress({ commit }, payload) { addAddress({ commit }, payload) {
console.log('@A addAddress payload', 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. if('newPostalCode' in payload){
//La confirmation est l'adresse elle-même. 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) } else {
.then(address => new Promise((resolve, reject) => { postAddress(payload)
commit('addAddress', address); .then(address => new Promise((resolve, reject) => {
resolve(); commit('addAddress', address);
})) resolve();
.catch((error) => { }))
commit('catchError', error); .catch((error) => {
}); commit('catchError', error);
});
}
} }
} }
}); });

View File

@ -66,7 +66,6 @@ const fetchAddresses = () => {
* @returns {Promise} * @returns {Promise}
*/ */
const postAddress = (address) => { const postAddress = (address) => {
console.log(address);
const url = `/api/1.0/main/address.json?`; const url = `/api/1.0/main/address.json?`;
const body = address; const body = address;
@ -91,7 +90,6 @@ const postAddress = (address) => {
* @body Object - dictionary with changes to post * @body Object - dictionary with changes to post
*/ */
const patchAddress = (id, body) => { const patchAddress = (id, body) => {
const url = `/api/1.0/main/address/${id}.json`; const url = `/api/1.0/main/address/${id}.json`;
return fetch(url, { return fetch(url, {
method: 'PATCH', 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 { export {
fetchCountries, fetchCountries,
@ -114,5 +131,6 @@ export {
fetchReferenceAddresses, fetchReferenceAddresses,
fetchAddresses, fetchAddresses,
postAddress, postAddress,
patchAddress patchAddress,
postPostalCode
}; };

View File

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

View File

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

View File

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

View File

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

View File

@ -200,6 +200,60 @@ paths:
description: "Unprocessable entity (validation errors)" description: "Unprocessable entity (validation errors)"
400: 400:
description: "transition cannot be applyed" 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: /1.0/main/address/{id}.json:
get: get:
@ -283,6 +337,35 @@ paths:
responses: responses:
200: 200:
description: "ok" 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: /1.0/main/postal-code/{id}.json:
get: get:
tags: tags:

View File

@ -85,7 +85,19 @@ export default {
}; };
if (address.selected.address.point !== undefined){ 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); this.$store.dispatch('addAddress', createdAddress);

View File

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