diff --git a/back/api/booking/controllers/booking.js b/back/api/booking/controllers/booking.js index a411645f..b189c8b3 100644 --- a/back/api/booking/controllers/booking.js +++ b/back/api/booking/controllers/booking.js @@ -26,7 +26,12 @@ const filterBookings = (type) => { switch (type) { case "request": return { - status_in: ["pending", "requestcanceled", "requestcanceledbyplace"], + status_in: [ + "pending", + "requestcanceled", + "requestcanceledbyplace", + "expired", + ], }; case "booking": return { diff --git a/back/api/booking/documentation/1.0.0/booking.json b/back/api/booking/documentation/1.0.0/booking.json index a2aacbcb..109908c6 100644 --- a/back/api/booking/documentation/1.0.0/booking.json +++ b/back/api/booking/documentation/1.0.0/booking.json @@ -753,7 +753,8 @@ "askcancel", "past", "accepted", - "pending" + "pending", + "expired" ] }, "messages": { @@ -1252,7 +1253,8 @@ "askcancel", "past", "accepted", - "pending" + "pending", + "expired" ] }, "messages": { diff --git a/back/api/booking/models/booking.js b/back/api/booking/models/booking.js index 17f01657..947f63f0 100644 --- a/back/api/booking/models/booking.js +++ b/back/api/booking/models/booking.js @@ -74,6 +74,10 @@ const checkStatus = (booking, status) => { module.exports = { lifecycles: { + async afterFindOne(result) { + const { status } = await strapi.services.booking.checkIsPast(result); + result.status = status; + }, async afterFind(results) { if (results.length > 0) { await Promise.all( @@ -135,7 +139,7 @@ module.exports = { ); }, beforeUpdate: async (params, data) => { - if (data.status) { + if (data.status && data.status !== "expired") { const booking = await strapi .query("booking") .findOne({ id: params.id }); diff --git a/back/api/booking/models/booking.settings.json b/back/api/booking/models/booking.settings.json index f05ed831..54483de0 100644 --- a/back/api/booking/models/booking.settings.json +++ b/back/api/booking/models/booking.settings.json @@ -24,7 +24,8 @@ "askcancel", "past", "accepted", - "pending" + "pending", + "expired" ] }, "messages": { diff --git a/back/api/booking/services/booking.js b/back/api/booking/services/booking.js index 767f3920..26ecfdcd 100644 --- a/back/api/booking/services/booking.js +++ b/back/api/booking/services/booking.js @@ -1,5 +1,6 @@ "use strict"; const max = require("date-fns/max"); +const min = require("date-fns/min"); const isPast = require("date-fns/isPast"); /** * Read the documentation (https://strapi.io/documentation/developer-docs/latest/development/backend-customization.html#core-services) @@ -7,8 +8,20 @@ const isPast = require("date-fns/isPast"); */ module.exports = { - checkIsPast(booking) { + async checkIsPast(booking) { if ( + booking.status === "pending" && + isPast(min(booking.disponibilities.map((dispo) => new Date(dispo.start)))) + ) { + await Promise.all( + booking.disponibilities.map((dispo) => + strapi.services.disponibility.checkStatus(dispo) + ) + ); + return strapi + .query("booking") + .update({ id: booking.id }, { status: "expired" }); + } else if ( booking.status === "accepted" && isPast(max(booking.disponibilities.map((dispo) => new Date(dispo.end)))) ) { diff --git a/back/api/disponibility/documentation/1.0.0/disponibility.json b/back/api/disponibility/documentation/1.0.0/disponibility.json index 710965ae..a76ed57d 100644 --- a/back/api/disponibility/documentation/1.0.0/disponibility.json +++ b/back/api/disponibility/documentation/1.0.0/disponibility.json @@ -795,7 +795,8 @@ "askcancel", "past", "accepted", - "pending" + "pending", + "expired" ] }, "messages": { diff --git a/back/api/disponibility/models/disponibility.settings.json b/back/api/disponibility/models/disponibility.settings.json index dd646313..cb1537f5 100644 --- a/back/api/disponibility/models/disponibility.settings.json +++ b/back/api/disponibility/models/disponibility.settings.json @@ -55,8 +55,8 @@ "required": true }, "booking": { - "model": "booking", - "via": "disponibilities" + "via": "disponibilities", + "model": "booking" }, "dispositif": { "via": "disponibilities", diff --git a/back/api/disponibility/services/disponibility.js b/back/api/disponibility/services/disponibility.js index 6538a8c8..6573355d 100644 --- a/back/api/disponibility/services/disponibility.js +++ b/back/api/disponibility/services/disponibility.js @@ -1,8 +1,21 @@ -'use strict'; - +"use strict"; +const isPast = require("date-fns/isPast"); /** * Read the documentation (https://strapi.io/documentation/developer-docs/latest/development/backend-customization.html#core-services) * to customize this service */ -module.exports = {}; +module.exports = { + async checkStatus(dispo) { + const data = {}; + if (isPast(new Date(dispo.start))) { + data["status"] = "past"; + } else { + data["status"] = "available"; + data["booking"] = null; + + await strapi.query("disponibility").create({ ...dispo, status: "past" }); + } + return strapi.query("disponibility").update({ id: dispo.id }, data); + }, +}; diff --git a/back/api/espace/documentation/1.0.0/espace.json b/back/api/espace/documentation/1.0.0/espace.json index 79b4ef4f..c1c7bdd6 100644 --- a/back/api/espace/documentation/1.0.0/espace.json +++ b/back/api/espace/documentation/1.0.0/espace.json @@ -1023,7 +1023,8 @@ "askcancel", "past", "accepted", - "pending" + "pending", + "expired" ] }, "messages": { diff --git a/back/api/espace/models/espace.settings.json b/back/api/espace/models/espace.settings.json index 26c6a9fb..2cdeef54 100644 --- a/back/api/espace/models/espace.settings.json +++ b/back/api/espace/models/espace.settings.json @@ -112,8 +112,8 @@ "default": true }, "bookings": { - "collection": "booking", - "via": "espace" + "via": "espace", + "collection": "booking" }, "country": { "type": "string", diff --git a/back/api/message/documentation/1.0.0/message.json b/back/api/message/documentation/1.0.0/message.json index 7eebc4b1..8312f78a 100644 --- a/back/api/message/documentation/1.0.0/message.json +++ b/back/api/message/documentation/1.0.0/message.json @@ -1096,7 +1096,8 @@ "askcancel", "past", "accepted", - "pending" + "pending", + "expired" ] }, "messages": { diff --git a/back/api/message/models/message.settings.json b/back/api/message/models/message.settings.json index ee267b92..12a28eb5 100644 --- a/back/api/message/models/message.settings.json +++ b/back/api/message/models/message.settings.json @@ -48,8 +48,8 @@ "required": true }, "booking": { - "model": "booking", - "via": "messages" + "via": "messages", + "model": "booking" }, "hasbeenread": { "type": "boolean", diff --git a/back/extensions/documentation/documentation/1.0.0/full_documentation.json b/back/extensions/documentation/documentation/1.0.0/full_documentation.json index ee0f70ff..5d2f9df6 100644 --- a/back/extensions/documentation/documentation/1.0.0/full_documentation.json +++ b/back/extensions/documentation/documentation/1.0.0/full_documentation.json @@ -14,7 +14,7 @@ "name": "Apache 2.0", "url": "https://www.apache.org/licenses/LICENSE-2.0.html" }, - "x-generation-date": "05/27/2021 1:51:58 PM" + "x-generation-date": "05/27/2021 3:44:21 PM" }, "x-strapi-config": { "path": "/documentation", @@ -9634,7 +9634,8 @@ "askcancel", "past", "accepted", - "pending" + "pending", + "expired" ] }, "messages": { @@ -10114,7 +10115,8 @@ "askcancel", "past", "accepted", - "pending" + "pending", + "expired" ] }, "messages": { diff --git a/web/components/Account/Booking/BookingDrawer.tsx b/web/components/Account/Booking/BookingDrawer.tsx index 3197b1b7..1ee58355 100644 --- a/web/components/Account/Booking/BookingDrawer.tsx +++ b/web/components/Account/Booking/BookingDrawer.tsx @@ -51,7 +51,7 @@ interface Props { const BookingDrawer = ({ bookingId, setSelected, type }: Props) => { const queryClient = useQueryClient() - const { data: user } = useCurrentUser() + const { data: user, isLoading: userLoading } = useCurrentUser() const { data: booking, isLoading } = useBooking(bookingId, { onSuccess: ({ id }) => { client.notifications @@ -88,7 +88,7 @@ const BookingDrawer = ({ bookingId, setSelected, type }: Props) => { /> - + { ) : ( - {format(dispo.end, 'd MMM yyyy')} + {format(dispo.start, 'd MMM yyyy')} {dispo.when && ( {`(${t(`${dispo.when}`)})`} @@ -208,7 +208,7 @@ const BookingDrawer = ({ bookingId, setSelected, type }: Props) => { opacity="0.3" /> - {user.type === 'company' ? ( + {user?.type === 'company' ? ( {t('structure')} @@ -255,11 +255,11 @@ const BookingDrawer = ({ bookingId, setSelected, type }: Props) => { direction="column" minW={{ base: 'none', - md: user.type === 'company' ? '250px' : '280px', + md: user?.type === 'company' ? '250px' : '280px', }} maxW={{ base: 'none', - md: user.type === 'company' ? '250px' : '280px', + md: user?.type === 'company' ? '250px' : '280px', }} w={{ base: '100%', md: 'fit-content' }} > @@ -288,7 +288,7 @@ const BookingDrawer = ({ bookingId, setSelected, type }: Props) => { {booking?.status === BookingStatus.PENDING && - user.type === 'place' && ( + user?.type === 'place' && ( { /> )} {booking?.status === BookingStatus.ACCEPTED && - user.type === 'company' && ( + user?.type === 'company' && ( { /> )} - {user.type === 'place' ? ( + {user?.type === 'place' ? ( ( ) -const BookingList = ({ bookings, type }: Props) => { +const BookingList = ({ bookings = [], type }: Props) => { const router = useRouter() const { data: user } = useCurrentUser() const { t } = useTranslation('booking') diff --git a/web/components/CircleStatus.tsx b/web/components/CircleStatus.tsx index 5ab80a4e..f9b93e49 100644 --- a/web/components/CircleStatus.tsx +++ b/web/components/CircleStatus.tsx @@ -13,6 +13,7 @@ interface Props extends BoxProps { | 'requestcanceledbyplace' | 'bookingcanceledbyplace' | 'occupied' + | 'expired' } const Circle = ({ bgColor, ...rest }) => { @@ -29,6 +30,7 @@ const Tag = ({ status, ...rest }: Props) => { case 'askcancel': return case 'past': + case 'expired': return case 'requestcanceled': case 'requestcanceledbyplace': diff --git a/web/components/Place/PlaceItinerary.tsx b/web/components/Place/PlaceItinerary.tsx index 8ae3f9ab..b1574794 100644 --- a/web/components/Place/PlaceItinerary.tsx +++ b/web/components/Place/PlaceItinerary.tsx @@ -9,23 +9,9 @@ interface Props { const PlaceItinerary = ({ place }: Props) => { const { t } = useTranslation('place') - const [isEnabled, setEnabled] = useState(true) const [link, setLink] = useState('') const ref = useRef() - useEffect(() => { - if (typeof navigator !== 'undefined' && 'permissions' in navigator) { - navigator.permissions - .query({ name: 'geolocation' }) - .then((res) => { - setEnabled(res.state === 'granted') - }) - .catch(() => { - setEnabled(false) - }) - } - }, []) - if (!('geolocation' in navigator)) return null return ( @@ -47,7 +33,6 @@ const PlaceItinerary = ({ place }: Props) => { onClick={() => { navigator.geolocation.getCurrentPosition( (position) => { - if (!isEnabled) setEnabled(true) const currentLatitude = position.coords.latitude const currentLongitude = position.coords.longitude setLink( @@ -59,7 +44,6 @@ const PlaceItinerary = ({ place }: Props) => { } }, (err) => { - setEnabled(false) console.log('err', err) }, { @@ -70,7 +54,7 @@ const PlaceItinerary = ({ place }: Props) => { ) }} > - {isEnabled ? t('detail.itinerary') : t('detail.notEnabled')} + {t('detail.itinerary')} )} diff --git a/web/components/Tag.tsx b/web/components/Tag.tsx index 9ef54bfd..c1ad3400 100644 --- a/web/components/Tag.tsx +++ b/web/components/Tag.tsx @@ -15,11 +15,13 @@ interface Props extends TagProps { | 'bookingcanceledbyplace' | 'occupied' | 'nextweek' + | 'expired' children?: React.ReactNode } const Tag = ({ status, children, ...rest }: Props) => { const { t } = useTranslation('account') + switch (status) { case 'booked': case 'available': @@ -47,6 +49,12 @@ const Tag = ({ status, children, ...rest }: Props) => { {children || t('tag.pending')} ) + case 'expired': + return ( + + {children || t('tag.expired')} + + ) case 'past': return ( diff --git a/web/public/locales/fr/account.json b/web/public/locales/fr/account.json index a64562aa..b5f42598 100644 --- a/web/public/locales/fr/account.json +++ b/web/public/locales/fr/account.json @@ -153,6 +153,7 @@ "past": "Passée", "occupied": "En cours", "askcancel": "Ann. demandée", + "expired": "Expirée", "newtweek": "Le semaine prochaine" }, "errors": {