Skip to content

Commit

Permalink
th-413: Add avatars to order card business, corrects markers, styles. (
Browse files Browse the repository at this point in the history
…BinaryStudioAcademy#426)

* th-413: * change some styles

* th-413: + getting avatar url

* th-413: + avatar to card

* th-413: * repair markers

* th-413: * repair markers

* th-413: * conflicts

* th-413: * dropdown

* th-413: * conflicts
  • Loading branch information
TetianaDoroshko committed Sep 29, 2023
1 parent 0a61402 commit 650c397
Show file tree
Hide file tree
Showing 14 changed files with 114 additions and 28 deletions.
27 changes: 26 additions & 1 deletion backend/src/packages/orders/order.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class OrderRepository implements Omit<IRepository, 'find'> {

private driversSchema: DatabaseSchema['drivers'];

private fileSchema: DatabaseSchema['files'];

public constructor(
database: Pick<IDatabase, 'driver'>,
{
Expand All @@ -37,9 +39,10 @@ class OrderRepository implements Omit<IRepository, 'find'> {
users,
shifts,
drivers,
files,
}: Pick<
DatabaseSchema,
'orders' | 'users' | 'trucks' | 'shifts' | 'drivers'
'orders' | 'users' | 'trucks' | 'shifts' | 'drivers' | 'files'
>,
) {
this.db = database;
Expand All @@ -48,6 +51,7 @@ class OrderRepository implements Omit<IRepository, 'find'> {
this.usersSchema = users;
this.shiftsSchema = shifts;
this.driversSchema = drivers;
this.fileSchema = files;
}

public async findById(id: OrderEntityT['id']): Promise<OrderEntityT | null> {
Expand All @@ -73,6 +77,7 @@ class OrderRepository implements Omit<IRepository, 'find'> {
email: this.usersSchema.email,
phone: this.usersSchema.phone,
driverLicenseNumber: this.driversSchema.driverLicenseNumber,
avatarUrl: this.fileSchema.key,
},
truck: {
id: this.shiftsSchema.truckId,
Expand All @@ -92,6 +97,10 @@ class OrderRepository implements Omit<IRepository, 'find'> {
this.driversSchema,
eq(this.driversSchema.userId, this.shiftsSchema.driverId),
)
.innerJoin(
this.fileSchema,
eq(this.driversSchema.avatarId, this.fileSchema.id),
)
.innerJoin(
this.trucksSchema,
eq(this.trucksSchema.id, this.shiftsSchema.truckId),
Expand Down Expand Up @@ -141,6 +150,7 @@ class OrderRepository implements Omit<IRepository, 'find'> {
email: this.usersSchema.email,
phone: this.usersSchema.phone,
driverLicenseNumber: this.driversSchema.driverLicenseNumber,
avatarUrl: this.fileSchema.key,
},
truck: {
id: this.shiftsSchema.truckId,
Expand All @@ -160,6 +170,10 @@ class OrderRepository implements Omit<IRepository, 'find'> {
this.driversSchema,
eq(this.driversSchema.userId, this.shiftsSchema.driverId),
)
.innerJoin(
this.fileSchema,
eq(this.driversSchema.avatarId, this.fileSchema.id),
)
.innerJoin(
this.trucksSchema,
eq(this.trucksSchema.id, this.shiftsSchema.truckId),
Expand Down Expand Up @@ -201,6 +215,7 @@ class OrderRepository implements Omit<IRepository, 'find'> {
email: this.usersSchema.email,
phone: this.usersSchema.phone,
driverLicenseNumber: this.driversSchema.driverLicenseNumber,
avatarUrl: this.fileSchema.key,
},
truck: {
id: this.shiftsSchema.truckId,
Expand All @@ -220,6 +235,10 @@ class OrderRepository implements Omit<IRepository, 'find'> {
this.driversSchema,
eq(this.driversSchema.userId, this.shiftsSchema.driverId),
)
.innerJoin(
this.fileSchema,
eq(this.driversSchema.avatarId, this.fileSchema.id),
)
.innerJoin(
this.trucksSchema,
eq(this.trucksSchema.id, this.shiftsSchema.truckId),
Expand Down Expand Up @@ -268,6 +287,7 @@ class OrderRepository implements Omit<IRepository, 'find'> {
email: this.usersSchema.email,
phone: this.usersSchema.phone,
driverLicenseNumber: this.driversSchema.driverLicenseNumber,
avatarUrl: this.fileSchema.key,
},
truck: {
id: this.shiftsSchema.truckId,
Expand All @@ -287,6 +307,10 @@ class OrderRepository implements Omit<IRepository, 'find'> {
this.driversSchema,
eq(this.driversSchema.userId, this.shiftsSchema.driverId),
)
.innerJoin(
this.fileSchema,
eq(this.driversSchema.avatarId, this.fileSchema.id),
)
.innerJoin(
this.trucksSchema,
eq(this.trucksSchema.id, this.shiftsSchema.truckId),
Expand Down Expand Up @@ -380,6 +404,7 @@ class OrderRepository implements Omit<IRepository, 'find'> {
public async getUserOrBusinessTotal(
search: Partial<{
ownerId: number | null;
businessId: OrderEntityT['businessId'];
status: OrderEntityT['status'];
}>,
): Promise<number> {
Expand Down
32 changes: 29 additions & 3 deletions backend/src/packages/orders/order.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { type SocketService } from '~/libs/packages/socket/socket.service.js';

import { type BusinessService } from '../business/business.service.js';
import { type DriverService } from '../drivers/driver.service.js';
import { getFilePublicUrl } from '../files/files.js';
import { type FilesService } from '../files/files.service.js';
import { type MapService } from '../map/map.service.js';
import { type ShiftService } from '../shifts/shift.service.js';
import { convertCurrencyToCents } from '../stripe/libs/helpers/helpers.js';
Expand Down Expand Up @@ -48,6 +50,8 @@ class OrderService implements Omit<IService, 'find'> {

private mapService: MapService;

private filesService: FilesService;

public constructor({
businessService,
orderRepository,
Expand All @@ -57,6 +61,7 @@ class OrderService implements Omit<IService, 'find'> {
userService,
mapService,
socket,
filesService,
}: {
orderRepository: OrderRepository;
businessService: BusinessService;
Expand All @@ -66,6 +71,7 @@ class OrderService implements Omit<IService, 'find'> {
userService: UserService;
mapService: MapService;
socket: SocketService;
filesService: FilesService;
}) {
this.orderRepository = orderRepository;

Expand All @@ -85,6 +91,8 @@ class OrderService implements Omit<IService, 'find'> {

this.socketService = socket;

this.filesService = filesService;

this.mapService = mapService;
}

Expand Down Expand Up @@ -123,6 +131,9 @@ class OrderService implements Omit<IService, 'find'> {
}

const driver = await this.userService.findById(shift.driverId);
const avatarUrl = shift.avatarId
? await this.filesService.findById(shift.avatarId)
: null;

if (!driver) {
throw new NotFoundError({
Expand Down Expand Up @@ -160,6 +171,7 @@ class OrderService implements Omit<IService, 'find'> {
email: driver.email,
phone: driver.phone,
driverLicenseNumber: shift.driverLicenseNumber,
avatarUrl: avatarUrl?.key ?? null,
},
truck: { id: truck.id, licensePlateNumber: truck.licensePlateNumber },
}).toObject();
Expand Down Expand Up @@ -255,6 +267,7 @@ class OrderService implements Omit<IService, 'find'> {
email: foundOrder.driver.email,
phone: foundOrder.driver.phone,
driverLicenseNumber: driver.driverLicenseNumber,
avatarUrl: driver.avatarUrl,
},
truck: { id: truck.id, licensePlateNumber: truck.licensePlateNumber },
};
Expand Down Expand Up @@ -348,15 +361,28 @@ class OrderService implements Omit<IService, 'find'> {

const total = query.status
? await this.orderRepository.getUserOrBusinessTotal({
ownerId: business.id,
businessId: business.id,
status: query.status,
})
: await this.orderRepository.getUserOrBusinessTotal({
ownerId: business.id,
businessId: business.id,
});

const items = usersOrders.map((it) => {
const order = OrderEntity.initialize(it).toObject();

if (order.shift.driver) {
const avatarUrl = order.shift.driver.avatarUrl;
order.shift.driver.avatarUrl = avatarUrl
? getFilePublicUrl(avatarUrl)
: null;
}

return order;
});

return {
items: usersOrders.map((it) => OrderEntity.initialize(it).toObject()),
items,
total,
};
}
Expand Down
2 changes: 2 additions & 0 deletions backend/src/packages/orders/orders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { socket } from '~/libs/packages/socket/socket.js';

import { businessService } from '../business/business.js';
import { driverService } from '../drivers/drivers.js';
import { filesService } from '../files/files.js';
import { mapService } from '../map/map.js';
import { shiftService } from '../shifts/shift.js';
import { truckService } from '../trucks/trucks.js';
Expand All @@ -22,6 +23,7 @@ const orderService = new OrderService({
userService,
mapService,
socket,
filesService,
});
const orderController = new OrderController({
logger,
Expand Down
11 changes: 7 additions & 4 deletions backend/src/packages/shifts/shift.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,12 @@ class ShiftRepository implements IRepository {
return shift;
}

public async getOpenedByTruckWithBusiness(
truckId: number,
): Promise<
| (ShiftDatabaseModel & { businessId: number; driverLicenseNumber: string })
public async getOpenedByTruckWithBusiness(truckId: number): Promise<
| (ShiftDatabaseModel & {
businessId: number;
driverLicenseNumber: string;
avatarId: number | null;
})
| null
> {
const [shift = null] = await this.db
Expand All @@ -91,6 +93,7 @@ class ShiftRepository implements IRepository {
...shift.shifts,
businessId: shift.driver_details.businessId,
driverLicenseNumber: shift.driver_details.driverLicenseNumber,
avatarId: shift.driver_details.avatarId,
}
: null;
}
Expand Down
12 changes: 8 additions & 4 deletions backend/src/packages/shifts/shift.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,13 @@ class ShiftService implements IService {
return shift ? ShiftEntity.initialize(shift).toObject() : null;
}

public async findOpenedByTruckWithBusiness(
truckId: number,
): Promise<
(ShiftEntityT & { businessId: number; driverLicenseNumber: string }) | null
public async findOpenedByTruckWithBusiness(truckId: number): Promise<
| (ShiftEntityT & {
businessId: number;
driverLicenseNumber: string;
avatarId: number | null;
})
| null
> {
const shiftDatabase =
await this.shiftRepository.getOpenedByTruckWithBusiness(truckId);
Expand All @@ -96,6 +99,7 @@ class ShiftService implements IService {
...shift,
businessId: shiftDatabase.businessId,
driverLicenseNumber: shiftDatabase.driverLicenseNumber,
avatarId: shiftDatabase.avatarId,
};
}

Expand Down
1 change: 0 additions & 1 deletion frontend/src/libs/components/dropdown/styles.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
.container {
.control {
height: 20px;
min-height: auto;
background: transparent;
border: none;
outline: none;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ const OrderListCardBusiness: React.FC<Properties> = ({

const statusBadge = mapOrderStatusToReadable[status];

const driverAratar = driver?.avatarUrl ?? ImgPath.AVATAR_DEFAULT;

return (
<div
className={styles.container}
Expand All @@ -47,11 +49,13 @@ const OrderListCardBusiness: React.FC<Properties> = ({
<p className={getValidClassNames('textMdBold', styles.cardName)}>
Order {id}
</p>
<Badge color={statusBadge.color}>{statusBadge.name}</Badge>
<Badge color={statusBadge.color} className={styles.badge}>
{statusBadge.name}
</Badge>
</div>
<div className={styles.content}>
<img
src={ImgPath.AVATAR_DEFAULT}
src={driverAratar}
alt={driver?.firstName}
className={styles.avatar}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
border-radius: 50%;
}

.badge {
width: 100px;
}

.driver {
display: flex;
margin-left: 25px;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const setMapService = ({
center: center,
zoom,
});
mapService.current.addMarker(center, false);
mapService.current.addMarkerStatic(center);
} else {
const bounds = getBounds(points);

Expand All @@ -41,7 +41,7 @@ const setMapService = ({
zoom,
});
for (const point of points) {
mapService.current.addMarker(point, false);
mapService.current.addMarkerStatic(point);
}
}
};
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/libs/hooks/use-app-map/use-app-map.hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ const useAppMap = ({

for (const point of points) {
mapService.current.removeMarkers();
mapService.current.addMarker(point, false);
mapService.current.addMarkerStatic(point);
}
}
}, [points]);
Expand Down
15 changes: 14 additions & 1 deletion frontend/src/libs/packages/map/map.package.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ImgPath } from '~/libs/enums/img-path.enum.js';
import { ApplicationError } from '~/libs/exceptions/exceptions.js';
import { type Coordinates } from '~/libs/types/types.js';

Expand Down Expand Up @@ -326,6 +327,18 @@ class MapService implements IMapService {
return marker;
}

public addMarkerStatic(
position: google.maps.LatLngLiteral,
): google.maps.Marker {
this.throwIfMapNotInitialized();

return new google.maps.Marker({
position,
map: this.map,
icon: ImgPath.TRUCK_SM,
});
}

public async addRoute({ startPoint, endPoint }: PlaceLatLng): Promise<void> {
this.throwIfMapNotInitialized();

Expand All @@ -347,7 +360,7 @@ class MapService implements IMapService {
startPoint,
endPoint,
}: PlaceLatLng): Promise<void> {
const anchor = this.addMarker(endPoint, false);
const anchor = this.addMarkerStatic(endPoint);

const startAddress = await this.getPointAddress(startPoint);
const endAddress = await this.getPointAddress(endPoint);
Expand Down
Loading

0 comments on commit 650c397

Please sign in to comment.