Skip to content

Commit e938fc3

Browse files
committed
Add image urls output to products and inventory items
1 parent 0006204 commit e938fc3

File tree

6 files changed

+22
-7
lines changed

6 files changed

+22
-7
lines changed

apps/api/src/helpers/dtoHelpers.ts

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import Utils from './utils';
2+
3+
class DtoHelpers {
4+
public static getImageUrl(key?: string): string {
5+
return Utils.getApiBaseUrl() + `images/${key || 'default'}`;
6+
}
7+
}
8+
9+
export default DtoHelpers;

apps/api/src/helpers/utils.ts

+4-7
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class Utils {
1010
return NODE_ENV == 'production';
1111
}
1212

13-
public static getApiBaseUrl(req?: Request): string {
13+
public static getApiBaseUrl(): string {
1414
const API_BASE_URL_REGEX = /^https?:\/\/.*api\/?$/;
1515
if (BASE_API_URL) {
1616
const isValid = API_BASE_URL_REGEX.test(BASE_API_URL);
@@ -20,13 +20,10 @@ class Utils {
2020
return BASE_API_URL.endsWith('/') ? BASE_API_URL : BASE_API_URL + '/';
2121
}
2222

23-
if (!req) {
24-
logger.error('BASE_API_URL have not been provided. Failed to retrieve absolute base URL');
25-
return '/api';
26-
}
23+
if (!Utils.isProduction()) return 'http://localhost:5173/api/';
2724

28-
logger.warn('BASE_API_URL have not been provided. Please update your .env file');
29-
return `${req.protocol}://${req.get('Host')}/api/`;
25+
logger.error('BASE_API_URL have not been provided. Failed to retrieve absolute base URL');
26+
return '/api';
3027
}
3128

3229
public static get schemaSerializerHelper(): SchemaOptions {

apps/api/src/models/inventory/schemas/inventory-item.schema.ts

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import mongoose, { HydratedDocument } from 'mongoose';
33
import { BasicInventoryItemDto, InventoryItemDto } from 'shared-types';
44
import { ProductDocument } from '../../products/schemas/product.schema';
55
import { Warehouse } from '../../warehouses/schemas/warehouse.schema';
6+
import DtoHelpers from '../../../helpers/dtoHelpers';
67

78
export type InventoryItemDocument = HydratedDocument<InventoryItem>;
89

@@ -26,6 +27,7 @@ export class InventoryItem {
2627
return {
2728
id: document._id,
2829
productId: isProductMongoId ? (document.product as any) : document.product._id.toString(),
30+
image: DtoHelpers.getImageUrl(document.product.imageKey),
2931
name: document.product.name,
3032
quantity: document.quantity,
3133
unit: document.product.unit,

apps/api/src/models/products/schemas/product.schema.ts

+5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
22
import mongoose, { HydratedDocument } from 'mongoose';
33
import { BasicProductDto, ProductDto } from 'shared-types';
44
import { Organization } from '../../organizations/schemas/organization.schema';
5+
import DtoHelpers from '../../../helpers/dtoHelpers';
56

67
export type ProductDocument = HydratedDocument<Product>;
78

@@ -13,6 +14,9 @@ export class Product {
1314
@Prop({ required: true, index: 'text' })
1415
name: string;
1516

17+
@Prop()
18+
imageKey: string;
19+
1620
@Prop()
1721
description: string;
1822

@@ -32,6 +36,7 @@ export class Product {
3236
return {
3337
id: document._id,
3438
name: document.name,
39+
image: DtoHelpers.getImageUrl(document.imageKey),
3540
buyPrice: document.buyPrice,
3641
sellPrice: document.sellPrice,
3742
unit: document.unit,

packages/shared-types/src/inventory/BasicInventoryItemDto.ts

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { BaseDto } from "../BaseDto";
33
export class BasicInventoryItemDto extends BaseDto {
44
productId: string;
55
name: string;
6+
image: string;
67
quantity: number;
78
unit: string;
89
}

packages/shared-types/src/product/BasicProductDto.ts

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { BaseDto } from "../BaseDto";
22

33
export class BasicProductDto extends BaseDto {
44
name: string;
5+
image: string;
56
buyPrice: number;
67
sellPrice: number;
78
unit?: string;

0 commit comments

Comments
 (0)