Skip to content

Commit

Permalink
work in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
mosbuma committed Apr 4, 2024
1 parent 24b1b4e commit 8ca3b0b
Show file tree
Hide file tree
Showing 12 changed files with 149 additions and 32 deletions.
28 changes: 28 additions & 0 deletions mysql-db/handy-queries.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
select UserID FROM security_users where UserName="[email protected]" into @marc;
select ID From fietsenstallingen where Title like "%Marktstraat%" AND Plaats="Apeldoorn" INTO @stalling;
select @marc, @stalling;
INSERT INTO security_users_sites(UserID, SiteID, isContact) VALUES(@marc, @stalling, 0);

select fs.title
from security_users_sites sus
JOIN fietsenstallingen fs ON (fs.ID=sus.SiteID)
where sus.UserID=@marc;

Select * from fietsenstallingen_services where FietsenstallingID="0066B68F-6F95-4C42-BACF7B44C50FA061";
Select * from fietsenstallingen where ID="0066B68F-6F95-4C42-BACF7B44C50FA061";

select ID, Title, Beheerder, BeheerderContact, Capacity from fietsenstallingen WHERE IsStationsstalling;
select * from fietsenstallingen WHERE ID="0066B68F-6F95-4C42-BACF7B44C50FA061";

select * from security_users_sites where UserID="D4351342-685D-D17A-B3617EEBBF39451C";
select * from security_users_sites where UserID = @marc;

select * from fietsenstalling_sectie where fietsenstallingsId=@SiteID;

select fs.ID, fs.Title, fs.Plaats, fs.Capacity, count(fss.sectieId), sum(fss.capaciteit)
from fietsenstalling_sectie fss
left join fietsenstallingen fs ON fs.id=fss.fietsenstallingsId
WHERE fs.ID="0066B68F-6F95-4C42-BACF7B44C50FA061"
group by fs.ID
having count(fss.sectieId)>1
order by fs.Plaats, fs.Title
32 changes: 30 additions & 2 deletions src/backend/services/fietsenstallingen-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,41 @@ import { prisma } from "~/server/db";
import type { fietsenstallingen } from "@prisma/client";
import type { ICrudService } from "~/backend/handlers/crud-service-interface";

BigInt.prototype.toJSON = function () {
const int = Number.parseInt(this.toString());
return int ?? this.toString();
};

const include = {
fietsenstalling_secties: {
include: {
secties_fietstype: {
include: { fietstype: true }
}
}
},
fietsenstallingen_services: {
include: {
services: true
}
}
}

// inspired by https://medium.com/@brandonlostboy/build-it-better-next-js-crud-api-b45d2e923896
const FietsenstallingenService: ICrudService<fietsenstallingen> = {
getAll: async () => {
return await prisma.fietsenstallingen.findMany();
return await prisma.fietsenstallingen.findMany({
include: {
fietsenstalling_secties: true,
}
});
},
getOne: async (id: string) => {
return await prisma.fietsenstallingen.findFirst({ where: { ID: id } });
return await prisma.fietsenstallingen.findFirst({
where: { ID: id },
include
}
);
},
create: async (_data: fietsenstallingen): Promise<fietsenstallingen> => {
return await prisma.fietsenstallingen.create({ data: _data });
Expand Down
8 changes: 5 additions & 3 deletions src/backend/services/fietsenstallingen_services-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ const FietsenstallingenServicesService: ICrudService<fietsenstallingen_services>
return await prisma.fietsenstallingen_services.findMany();
},
getOne: async (fietsenstallingId: string) => {
return await prisma.fietsenstallingen_services.findFirst({ where: {
FietsenstallingID: fietsenstallingId
} });
return await prisma.fietsenstallingen_services.findFirst({
where: {
FietsenstallingID: fietsenstallingId
}
});
},
create: async (_data: fietsenstallingen_services): Promise<fietsenstallingen_services> => {
return await prisma.fietsenstallingen_services.create({ data: _data });
Expand Down
19 changes: 12 additions & 7 deletions src/components/parking/ParkingEdit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import { Tabs, Tab, FormHelperText, Typography } from "@mui/material";

/* Use nicely formatted items for items that can not be changed yet */
import ParkingViewTarief from "~/components/parking/ParkingViewTarief";
import type { ServiceType } from "~/components/parking/ParkingViewServices";

import ParkingViewAbonnementen from "~/components/parking/ParkingViewAbonnementen";
import ParkingEditCapaciteit, { type CapaciteitType } from "~/components/parking/ParkingEditCapaciteit";
import ParkingEditLocation from "~/components/parking/ParkingEditLocation";
Expand All @@ -36,8 +38,8 @@ export type ParkingEditUpdateStructure = {
Postcode?: string;
Plaats?: string;
Coordinaten?: string;
DateCreated: Date;
DateModified: Date;
DateCreated?: Date;
DateModified?: Date;
Type?: string;
SiteID?: string;
Beheerder?: string,
Expand All @@ -48,7 +50,6 @@ export type ParkingEditUpdateStructure = {
fietsenstalling_secties?: ParkingSections; // Replace with the actual type if different
}

type ServiceType = { ID: string, Name: string };
type ChangedType = { ID: string, selected: boolean };

const NoClickOverlay = () => {
Expand Down Expand Up @@ -237,11 +238,11 @@ const ParkingEdit = ({ parkingdata, onClose, onChange }: { parkingdata: ParkingD
let checks: checkInfo[] = [
{ type: "string", text: "invoer van de titel", value: parkingdata.Title, newvalue: newTitle },
{ type: "string", text: "invoer van de straat en huisnummer", value: parkingdata.Location, newvalue: newLocation },
{ type: "string", text: "invoer van de postcode", value: parkingdata.Postcode, newvalue: newPostcode },
{ type: "string", text: "invoer van de plaats", value: parkingdata.Plaats, newvalue: newPlaats },
{ type: "string", text: "selectie van de gemeente", value: parkingdata.SiteID, newvalue: newSiteID },
{ type: "coordinaten", text: "instellen van de locatie op de kaart", value: parkingdata.Coordinaten, newvalue: newCoordinaten },
]
// parkingdata.Postcode is optional

// FMS & ExploitantID cannot be changed for now, so no need to check those for changes
if (parkingdata.FMS !== true && parkingdata.ExploitantID === null) {
Expand Down Expand Up @@ -311,6 +312,7 @@ const ParkingEdit = ({ parkingdata, onClose, onChange }: { parkingdata: ParkingD
if (!parkingdata.DateCreated) {
update.DateCreated = today;
}

update.DateModified = today;

return update;
Expand Down Expand Up @@ -363,6 +365,7 @@ const ParkingEdit = ({ parkingdata, onClose, onChange }: { parkingdata: ParkingD
}

const updateCapaciteit = async (parkingdata: ParkingDetailsType, newCapaciteit: ParkingSections) => {
console.log("update capaciteit", newCapaciteit);
if (!newCapaciteit || newCapaciteit.length <= 0) return;

try {
Expand Down Expand Up @@ -541,8 +544,10 @@ const ParkingEdit = ({ parkingdata, onClose, onChange }: { parkingdata: ParkingD
return;
}


const method = isNew ? "POST" : "PUT";
const body = JSON.stringify(isNew ? Object.assign({}, parkingdata, update) : update);
console.log("update %s / %s", isNew, method, update);

const result = await fetch(
"/api/fietsenstallingen?id=" + parkingdata.ID,
Expand All @@ -564,9 +569,9 @@ const ParkingEdit = ({ parkingdata, onClose, onChange }: { parkingdata: ParkingD
}

// If capaciteit is updated: Update capaciteit
if (newCapaciteit && newCapaciteit.length > 0) {
await updateCapaciteit(parkingdata, newCapaciteit);
}
// if (newCapaciteit && newCapaciteit.length > 0) {
// await updateCapaciteit(parkingdata, newCapaciteit);
// }

let returnID: string | boolean = parkingdata.ID
if (session === null) {
Expand Down
4 changes: 3 additions & 1 deletion src/components/parking/ParkingEditCapaciteit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,9 @@ const toggleActive = (fietsenstalling_secties: ParkingSections, fietstypeName: s

const handleCapacityChange = (fietsenstalling_secties: ParkingSections, fietstypeName: any, amountstr: string): ParkingSections => {
// It's mandatory to have at least 1 section
if (!fietsenstalling_secties) return fietsenstalling_secties;
if (!fietsenstalling_secties) {
return fietsenstalling_secties;
}
if (!fietsenstalling_secties[0]) return fietsenstalling_secties;
if (!fietsenstalling_secties[0].secties_fietstype) return fietsenstalling_secties;

Expand Down
6 changes: 3 additions & 3 deletions src/components/parking/ParkingView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ const ParkingView = ({
<ParkingViewAbonnementen parkingdata={parkingdata} />

<SectionBlock heading="Soort stalling">
{parkingdata.fietsenstalling_type?.name || "Onbekend"}
{parkingdata.Type || "Onbekend"}
</SectionBlock>

<HorizontalDivider className="my-4" />
Expand All @@ -141,7 +141,7 @@ const ParkingView = ({
sm:absolute
sm:bottom-1
"
onClick={(e) => {
onClick={(e: any) => {
if (e) e.preventDefault();
openRoute(parkingdata.Coordinaten);
}}
Expand All @@ -158,7 +158,7 @@ const ParkingView = ({
</div>
</div>

</div>
</div >
);
};

Expand Down
26 changes: 18 additions & 8 deletions src/components/parking/ParkingViewBeheerder.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,31 @@ import HorizontalDivider from "~/components/HorizontalDivider";
import SectionBlock from "~/components/SectionBlock";

const ParkingViewBeheerder = ({ parkingdata }: { parkingdata: any }) => {
// console.log("### ParkingViewBeheerder", parkingdata, parkingdata.Exploitant, parkingdata.Beheerder, parkingdata.BeheerderContact);
if (parkingdata.FMS === true) {
return <SectionBlock heading="Beheerder">FMS</SectionBlock>;
} else if(parkingdata?.exploitant) {
console.log("### ParkingViewBeheerder", parkingdata, parkingdata.Exploitant, parkingdata.Beheerder, parkingdata.BeheerderContact);
// if (parkingdata.FMS === true) {
// return <SectionBlock heading="Beheerder">FMS</SectionBlock>;
// } else
if (parkingdata?.exploitant) {
return (
<SectionBlock heading="Beheerder">
<a href={'mailto:'+parkingdata.exploitant.Helpdesk}>{parkingdata.exploitant.CompanyName}</a>
<a href={'mailto:' + parkingdata.exploitant.Helpdesk}>{parkingdata.exploitant.CompanyName}</a>
</SectionBlock>
)
} else if(parkingdata.BeheerderContact !== null) {
} else if (parkingdata.BeheerderContact !== null) {
let contactlink = "";
if (parkingdata.BeheerderContact.includes("@")) {
contactlink = 'mailto:' + parkingdata.BeheerderContact
} else if (parkingdata.BeheerderContact.startsWith("http")) {
contactlink = parkingdata.BeheerderContact;
} else if (parkingdata.BeheerderContact.startsWith("www")) {
contactlink = 'https://' + parkingdata.BeheerderContact;
}

return (
<SectionBlock heading="Beheerder">
<a href={parkingdata.BeheerderContact}>{parkingdata.Beheerder === null ? 'contact' : parkingdata.Beheerder}</a>
<a href={contactlink}>{parkingdata.Beheerder === null ? parkingdata.BeheerderContact : parkingdata.Beheerder}</a>
</SectionBlock>
);
);
} else {
return null
}
Expand Down
21 changes: 15 additions & 6 deletions src/components/parking/ParkingViewCapaciteit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@ const calculateCapacityData = (parking: ParkingDetailsType): capacitydata | null
detailed: {},
};

if (parking === null || parking.Capacity === 0) {
capacity.unknown = true;
if (parking === null) {
capacity.unknown = true
} else if (parking.fietsenstalling_secties.length === 0) {
capacity.unknown = false;
capacity.total = parking.Capacity || 0;
} else {
// Get parking section (new: 1 per parking, to make it easy)
parking.fietsenstalling_secties.forEach((sectie) => {
Expand All @@ -41,7 +44,7 @@ const calculateCapacityData = (parking: ParkingDetailsType): capacitydata | null
let detailed = capacity.detailed[name];
if (detailed !== undefined) {
detailed.Toegestaan = detailed.Toegestaan || (data.Toegestaan !== null && data.Toegestaan);
detailed.Capaciteit += detailed.Capaciteit || 0;
detailed.Capaciteit += data.Capaciteit || 0;
}
// capacity.detailed[name].Toegestaan = capacity.detailed[name].Toegestaan || data.Toegestaan !== null && data.Toegestaan;
// capacity.detailed[name].Capaciteit += data.Capaciteit || 0;
Expand All @@ -63,9 +66,11 @@ const ParkingViewCapaciteit = ({ parkingdata }: { parkingdata: ParkingDetailsTyp
const capacitydata = calculateCapacityData(parkingdata);
// console.log("#### capacitydata", capacitydata, parkingdata);

if (capacitydata === null || capacitydata?.unknown) {
content = "Onbekend";
} else if (capacitydata.detailed === null || Object.keys(capacitydata.detailed).length === 0) {
if (capacitydata === null || capacitydata?.unknown || (Object.keys(capacitydata.detailed).length === 0 && capacitydata.total === 0)) {
return null;
}

if (capacitydata.detailed === null || Object.keys(capacitydata.detailed).length === 0) {
content = (
<>
<div className="">{parkingdata.Capacity}</div>
Expand Down Expand Up @@ -98,6 +103,10 @@ const ParkingViewCapaciteit = ({ parkingdata }: { parkingdata: ParkingDetailsTyp
});
}

if (content === null) {
return null;
}

return (
<>
<SectionBlock heading="Capaciteit">
Expand Down
8 changes: 8 additions & 0 deletions src/components/parking/ParkingViewServices.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
getAllServices
} from "~/utils/parkings";

export type ServiceType = { ID: string, Name: string };

const ParkingViewServices = ({ parkingdata }: { parkingdata: any }) => {
const [allServices, setAllServices] = React.useState<ServiceType[]>([]);

Expand All @@ -32,6 +34,12 @@ const ParkingViewServices = ({ parkingdata }: { parkingdata: any }) => {
return null
}

const activeServices = allServices && allServices.filter((service: any) => serviceIsActive(service)) || [];
if (activeServices.length === 0) {
// dont show services header if there are none
return null;
}

return <>
<SectionBlock heading="Services">
<div className="flex-1" key={'services' + parkingdata.ID}>
Expand Down
2 changes: 1 addition & 1 deletion src/pages/api/auth/[...nextauth].ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { Provider } from "next-auth/providers";
import NextAuth from "next-auth";
// import { PrismaAdapter } from "@auth/prisma-adapter"

import type { NextAuthOptions, User } from "next-auth";
import type { NextAuthOptions, RequestInternal, User } from "next-auth";
// import EmailProvider from "next-auth/providers/email"

import CredentialsProvider from "next-auth/providers/credentials";
Expand Down
1 change: 0 additions & 1 deletion src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ export async function getServerSideProps(context: any) {
const fietsenstallingen: fietsenstallingen[] = await getParkingsFromDatabase(sites);

// TODO: Don't include: EditorCreated, EditorModified
console.log("###### sites", sites)

return {
props: {
Expand Down
26 changes: 26 additions & 0 deletions src/utils/parkings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,30 @@ export const getDefaultLocation = (): string => {
return '52.09066,5.121317'
}

// const getNewStallingSectieDefaultRecord = () => {
// const sectieId = generateRandomId();

// return {
// sectieId,
// externalId: "",
// titel: "sectie 1",
// omschrijving: "",
// capaciteit: 0,
// CapaciteitBromfiets: 0,
// kleur: "00FF00",
// // fietsenstallingsId: null,
// isKluis: 0,
// reserveringskostenPerDag: null,
// urlwebservice: 0,
// Reservable: 0,
// NotaVerwijssysteem: null,
// Bezetting: 0,
// isactief: 1,
// qualificatie: "NONE",
// secties_fietstype: []
// }
// }

export const getNewStallingDefaultRecord = (ID: string, latlong?: string[] | undefined): ParkingDetailsType => {
const data: ParkingDetailsType = {
ID,
Expand Down Expand Up @@ -196,6 +220,8 @@ export const getNewStallingDefaultRecord = (ID: string, latlong?: string[] | und
FMS: false,
Beheerder: "",
BeheerderContact: "",

// fietsenstalling_secties: [getNewStallingSectieDefaultRecord()],
}

return data
Expand Down

0 comments on commit 8ca3b0b

Please sign in to comment.