Skip to content

Commit

Permalink
expiration booking
Browse files Browse the repository at this point in the history
  • Loading branch information
keery committed May 27, 2021
1 parent 46d8929 commit 99612dd
Show file tree
Hide file tree
Showing 19 changed files with 86 additions and 48 deletions.
7 changes: 6 additions & 1 deletion back/api/booking/controllers/booking.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 4 additions & 2 deletions back/api/booking/documentation/1.0.0/booking.json
Original file line number Diff line number Diff line change
Expand Up @@ -753,7 +753,8 @@
"askcancel",
"past",
"accepted",
"pending"
"pending",
"expired"
]
},
"messages": {
Expand Down Expand Up @@ -1252,7 +1253,8 @@
"askcancel",
"past",
"accepted",
"pending"
"pending",
"expired"
]
},
"messages": {
Expand Down
6 changes: 5 additions & 1 deletion back/api/booking/models/booking.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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 });
Expand Down
3 changes: 2 additions & 1 deletion back/api/booking/models/booking.settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"askcancel",
"past",
"accepted",
"pending"
"pending",
"expired"
]
},
"messages": {
Expand Down
15 changes: 14 additions & 1 deletion back/api/booking/services/booking.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
"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)
* to customize this service
*/

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))))
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -795,7 +795,8 @@
"askcancel",
"past",
"accepted",
"pending"
"pending",
"expired"
]
},
"messages": {
Expand Down
4 changes: 2 additions & 2 deletions back/api/disponibility/models/disponibility.settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@
"required": true
},
"booking": {
"model": "booking",
"via": "disponibilities"
"via": "disponibilities",
"model": "booking"
},
"dispositif": {
"via": "disponibilities",
Expand Down
19 changes: 16 additions & 3 deletions back/api/disponibility/services/disponibility.js
Original file line number Diff line number Diff line change
@@ -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);
},
};
3 changes: 2 additions & 1 deletion back/api/espace/documentation/1.0.0/espace.json
Original file line number Diff line number Diff line change
Expand Up @@ -1023,7 +1023,8 @@
"askcancel",
"past",
"accepted",
"pending"
"pending",
"expired"
]
},
"messages": {
Expand Down
4 changes: 2 additions & 2 deletions back/api/espace/models/espace.settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@
"default": true
},
"bookings": {
"collection": "booking",
"via": "espace"
"via": "espace",
"collection": "booking"
},
"country": {
"type": "string",
Expand Down
3 changes: 2 additions & 1 deletion back/api/message/documentation/1.0.0/message.json
Original file line number Diff line number Diff line change
Expand Up @@ -1096,7 +1096,8 @@
"askcancel",
"past",
"accepted",
"pending"
"pending",
"expired"
]
},
"messages": {
Expand Down
4 changes: 2 additions & 2 deletions back/api/message/models/message.settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@
"required": true
},
"booking": {
"model": "booking",
"via": "messages"
"via": "messages",
"model": "booking"
},
"hasbeenread": {
"type": "boolean",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -9634,7 +9634,8 @@
"askcancel",
"past",
"accepted",
"pending"
"pending",
"expired"
]
},
"messages": {
Expand Down Expand Up @@ -10114,7 +10115,8 @@
"askcancel",
"past",
"accepted",
"pending"
"pending",
"expired"
]
},
"messages": {
Expand Down
18 changes: 9 additions & 9 deletions web/components/Account/Booking/BookingDrawer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -88,7 +88,7 @@ const BookingDrawer = ({ bookingId, setSelected, type }: Props) => {
/>
<DrawerOverlay bgColor="rgb(255 255 255 / 67%)">
<DrawerContent>
<Loading isLoading={isLoading} isCentered>
<Loading isLoading={isLoading || userLoading} isCentered>
<UrlRewrite
id={bookingId}
path={
Expand Down Expand Up @@ -156,7 +156,7 @@ const BookingDrawer = ({ bookingId, setSelected, type }: Props) => {
) : (
<Box>
<Flex alignItems="center">
<Text>{format(dispo.end, 'd MMM yyyy')}</Text>
<Text>{format(dispo.start, 'd MMM yyyy')}</Text>
{dispo.when && (
<Text textTransform="lowercase" pl={1.5}>
{`(${t(`${dispo.when}`)})`}
Expand Down Expand Up @@ -208,7 +208,7 @@ const BookingDrawer = ({ bookingId, setSelected, type }: Props) => {
opacity="0.3"
/>
</Box>
{user.type === 'company' ? (
{user?.type === 'company' ? (
<Box flex={1} minW="250px" maxW="250px">
<Text fontFamily="mabry medium" fontWeight="500">
{t('structure')}
Expand Down Expand Up @@ -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' }}
>
Expand Down Expand Up @@ -288,7 +288,7 @@ const BookingDrawer = ({ bookingId, setSelected, type }: Props) => {
</Button>
</Link>
{booking?.status === BookingStatus.PENDING &&
user.type === 'place' && (
user?.type === 'place' && (
<ConfirmModal
bookingId={booking?.id}
setSelected={setSelected}
Expand All @@ -302,7 +302,7 @@ const BookingDrawer = ({ bookingId, setSelected, type }: Props) => {
/>
)}
{booking?.status === BookingStatus.ACCEPTED &&
user.type === 'company' && (
user?.type === 'company' && (
<AskCancelModal
booking={booking}
setSelected={setSelected}
Expand All @@ -322,7 +322,7 @@ const BookingDrawer = ({ bookingId, setSelected, type }: Props) => {
/>
)}
<Divider opacity="0.3" my={5} />
{user.type === 'place' ? (
{user?.type === 'place' ? (
<BookingDrawerCompany
company={booking?.company}
espace={booking?.espace}
Expand Down
2 changes: 1 addition & 1 deletion web/components/Account/Booking/BookingList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const Divider = (props: DividerProps) => (
<ChakraDivider orientation="vertical" h="24px" mr={2.5} {...props} />
)

const BookingList = ({ bookings, type }: Props) => {
const BookingList = ({ bookings = [], type }: Props) => {
const router = useRouter()
const { data: user } = useCurrentUser()
const { t } = useTranslation('booking')
Expand Down
2 changes: 2 additions & 0 deletions web/components/CircleStatus.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ interface Props extends BoxProps {
| 'requestcanceledbyplace'
| 'bookingcanceledbyplace'
| 'occupied'
| 'expired'
}

const Circle = ({ bgColor, ...rest }) => {
Expand All @@ -29,6 +30,7 @@ const Tag = ({ status, ...rest }: Props) => {
case 'askcancel':
return <Circle bgColor="tag.yellow" {...rest} />
case 'past':
case 'expired':
return <Circle bgColor="tag.gray" {...rest} />
case 'requestcanceled':
case 'requestcanceledbyplace':
Expand Down
18 changes: 1 addition & 17 deletions web/components/Place/PlaceItinerary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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(
Expand All @@ -59,7 +44,6 @@ const PlaceItinerary = ({ place }: Props) => {
}
},
(err) => {
setEnabled(false)
console.log('err', err)
},
{
Expand All @@ -70,7 +54,7 @@ const PlaceItinerary = ({ place }: Props) => {
)
}}
>
{isEnabled ? t('detail.itinerary') : t('detail.notEnabled')}
{t('detail.itinerary')}
</Button>
)}
</Box>
Expand Down
8 changes: 8 additions & 0 deletions web/components/Tag.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand Down Expand Up @@ -47,6 +49,12 @@ const Tag = ({ status, children, ...rest }: Props) => {
{children || t('tag.pending')}
</ChakraTag>
)
case 'expired':
return (
<ChakraTag bgColor="tag.gray" {...rest}>
{children || t('tag.expired')}
</ChakraTag>
)
case 'past':
return (
<ChakraTag bgColor="tag.gray" {...rest}>
Expand Down
1 change: 1 addition & 0 deletions web/public/locales/fr/account.json
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@
"past": "Passée",
"occupied": "En cours",
"askcancel": "Ann. demandée",
"expired": "Expirée",
"newtweek": "Le semaine prochaine"
},
"errors": {
Expand Down

0 comments on commit 99612dd

Please sign in to comment.