Skip to content

Commit

Permalink
Add org settings to backend
Browse files Browse the repository at this point in the history
  • Loading branch information
MrBartusek committed Nov 23, 2023
1 parent 5a2a94a commit f713f5b
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 3 deletions.
17 changes: 15 additions & 2 deletions apps/api/src/models/organizations/organizations.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
Get,
NotFoundException,
Param,
Patch,
Post,
Req,
UseGuards,
Expand All @@ -14,12 +15,15 @@ import {
CreateOrganizationDto,
CreateWarehouseInOrgDto,
OrganizationDto,
PatchOrganizationSettingsDto,
WarehouseDto,
} from 'shared-types';
import { AuthenticatedGuard } from '../../auth/guards/authenticated.guard';
import { Warehouse } from '../warehouses/schemas/warehouse.schema';
import { OrganizationsService } from './organizations.service';
import { Organization } from './schemas/organization.schema';
import { ParseObjectIdPipe } from '../../pipes/prase-object-id.pipe';
import { Types } from 'mongoose';

@Controller('organizations')
@UseGuards(AuthenticatedGuard)
Expand Down Expand Up @@ -60,7 +64,7 @@ export class OrganizationsController {
}

@Get(':id')
async findByID(@Param('id') id: string): Promise<OrganizationDto> {
async findByID(@Param('id', ParseObjectIdPipe) id: Types.ObjectId): Promise<OrganizationDto> {
const org = await this.organizationsService.findById(id);
if (!org) {
throw new NotFoundException();
Expand All @@ -69,8 +73,17 @@ export class OrganizationsController {
}

@Get(':id/warehouses')
async findWarehouses(@Param('id') id: string): Promise<any> {
async findWarehouses(@Param('id', ParseObjectIdPipe) id: Types.ObjectId): Promise<any> {
const warehouses = await this.organizationsService.findAllWarehouses(id);
return warehouses.map((w) => Warehouse.toDto(w));
}

@Patch('settings')
async updateSettings(
@Param('id', ParseObjectIdPipe) id: Types.ObjectId,
@Body(new ValidationPipe()) patchOrganizationSettingsDto: PatchOrganizationSettingsDto,
): Promise<OrganizationDto> {
const org = await this.organizationsService.updateSettings(id, patchOrganizationSettingsDto);
return Organization.toDto(org);
}
}
9 changes: 9 additions & 0 deletions apps/api/src/models/organizations/organizations.service.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { Injectable } from '@nestjs/common';
import * as mongoose from 'mongoose';
import { FilterQuery } from 'mongoose';
import { CreateOrganizationDto, CreateWarehouseDto } from 'shared-types';
import { WarehouseDocument } from '../warehouses/schemas/warehouse.schema';
import { WarehousesService } from '../warehouses/warehouses.service';
import { OrganizationRepository } from './organizations.repository';
import { OrgSettingsDocument } from './schemas/org-settings';
import { OrganizationDocument } from './schemas/organization.schema';

@Injectable()
Expand Down Expand Up @@ -114,4 +116,11 @@ export class OrganizationsService {
{ stats: { totalPendingOrders: count } },
);
}

async updateSettings(
id: mongoose.Types.ObjectId | string,
settings: FilterQuery<OrgSettingsDocument>,
) {
return this.organizationRepository.findOneAndUpdate({ _id: id }, { settings: settings });
}
}
21 changes: 21 additions & 0 deletions apps/api/src/models/organizations/schemas/org-settings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { HydratedDocument } from 'mongoose';

export type OrgSettingsDocument = HydratedDocument<OrgSettings>;

export enum OrgValueCalculationStrategy {
BuyPrice = 'buyPrice',
SellPrice = 'sellPrice',
}

@Schema({ _id: false })
export class OrgSettings {
@Prop({
type: String,
enum: Object.values(OrgValueCalculationStrategy),
default: OrgValueCalculationStrategy.SellPrice,
})
valueCalculationStrategy: OrgValueCalculationStrategy;
}

export const OrgSettingsSchema = SchemaFactory.createForClass(OrgSettings);
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
OrgWarehouseReference,
OrgWarehouseReferenceSchema,
} from './org-warehouse-reference.schema';
import { OrgSettings, OrgSettingsSchema } from './org-settings';

export type OrganizationDocument = HydratedDocument<Organization>;

Expand All @@ -21,6 +22,9 @@ export class Organization {
@Prop({ type: OrgStatsSchema, default: {} })
stats: OrgStats;

@Prop({ type: OrgSettingsSchema, default: {} })
settings: OrgSettings;

@Prop({ type: [OrgAclSchema], default: [] })
acls: OrgAcl[];

Expand All @@ -33,6 +37,7 @@ export class Organization {
name: document.name,
currency: document.currency,
stats: document.stats,
settings: document.settings,
warehouses: document.warehouses,
};
}
Expand Down
4 changes: 3 additions & 1 deletion packages/shared-types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { CreateProductDto } from './product/CreateProductDto'
import { BasicInventoryItemDto } from './inventory/BasicInventoryItemDto'
import { InventoryItemDto } from './inventory/InventoryItemDto'
import { AddInventoryItemDto } from './inventory/AddInventoryItemDto'
import { PatchOrganizationSettingsDto } from './organizations/PatchOrganizationSettingsDto'

export {
BasicProductDto,
Expand All @@ -33,5 +34,6 @@ export {
CreateProductDto,
BasicInventoryItemDto,
InventoryItemDto,
AddInventoryItemDto
AddInventoryItemDto,
PatchOrganizationSettingsDto
}
3 changes: 3 additions & 0 deletions packages/shared-types/src/organizations/OrganizationDto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,8 @@ export class OrganizationDto extends BaseDto {
totalValue: number,
totalPendingOrders: number,
}
settings: {
valueCalculationStrategy: 'buyPrice' | 'sellPrice'
}
warehouses: BasicWarehouseDto[];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { IsIn, IsOptional } from "class-validator";

export class PatchOrganizationSettingsDto {
@IsOptional()
@IsIn(['buyPrice', 'sellPrice'])
valueCalculationStrategy?: string;
}

0 comments on commit f713f5b

Please sign in to comment.