Skip to content
This repository has been archived by the owner on Dec 14, 2023. It is now read-only.

schemas with decorators #128

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions elevate.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ if (commandLineArgs.length > 1) {
useNewUrlParser: true,
useFindAndModify: false,
useUnifiedTopology: true,
}
};
mongoose.connect(url, connectOptions).then();

const UserPermissionsSchema = new mongoose.Schema({
Expand All @@ -29,16 +29,19 @@ if (commandLineArgs.length > 1) {

console.log(`Elevating ${userId} to ${role}.`);

const filterKwargs = {_id: userId};
const userPermissions = {_id: userId, legalEntityId: legalEntityId, role: role};
UserPermissionsModel.updateOne(filterKwargs, userPermissions).then(() => {
console.log(`Successfully elevated ${userId} to ${role}.`);
process.exit();
}, () => {
console.log(`Failed to elevate ${userId} to ${role}.`);
process.exit();
});
const filterKwargs = { _id: userId };
const userPermissions = { _id: userId, legalEntityId: legalEntityId, role: role };
UserPermissionsModel.updateOne(filterKwargs, userPermissions).then(
() => {
console.log(`Successfully elevated ${userId} to ${role}.`);
process.exit();
},
() => {
console.log(`Failed to elevate ${userId} to ${role}.`);
process.exit();
},
);
} else {
console.log('Supply command line arguments (1) email (2) role.');
process.exit();
}
}
7 changes: 6 additions & 1 deletion src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ mongoose.set('useFindAndModify', false);
QueryModule,
CommandModule,
EventProcessingModule,
MongooseModule.forRoot(`mongodb://${host}:${port}/${database}`),
MongooseModule.forRoot(`mongodb://${host}:${port}/${database}`, {
useCreateIndex: true,
useNewUrlParser: true,
useFindAndModify: false,
useUnifiedTopology: true,
}),
TerminusModule,
],
controllers: [HealthController],
Expand Down
4 changes: 2 additions & 2 deletions src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Injectable, Logger } from '@nestjs/common';
import { CommandBus } from '@nestjs/cqrs';
import { RegisterOidcUserCommand } from '../commons/commands/register-oidc-user.command';
import { UserQueryService } from '../commons/user/user.qry-service';
import { IUser } from '../commons/user/user.schema';
import { User } from '../commons/user/user.schema';

@Injectable()
export class AuthService {
Expand All @@ -11,7 +11,7 @@ export class AuthService {
constructor(private commandBus: CommandBus, private userQryService: UserQueryService) {}

async createOrLogin(idToken: Record<string, any>): Promise<string> {
const user: IUser = await this.userQryService.retrieveUser(idToken.sub).then(async localScopeUser => {
const user: User = await this.userQryService.retrieveUser(idToken.sub).then(async localScopeUser => {
if (!localScopeUser) {
await this.postRegisterCommand(idToken);
return this.userQryService.retrieveUser(idToken.sub);
Expand Down
4 changes: 2 additions & 2 deletions src/auth/jwt.strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { Injectable, Logger } from '@nestjs/common';
import { PassportStrategy } from '@nestjs/passport';
import { passportJwtSecret } from 'jwks-rsa';
import { ExtractJwt, Strategy } from 'passport-jwt';
import { UserPermissions } from '../commons/user/user-permissions.schema';
import { UserQueryService } from '../commons/user/user.qry-service';
import { IUserPermissions } from '../commons/user/user.schema';
import { AuthService } from './auth.service';
import { ValidatedUser } from './validated-user';

Expand Down Expand Up @@ -34,7 +34,7 @@ export class JwtStrategy extends PassportStrategy(Strategy, 'jwt') {
const userId: string = await this.authService.createOrLogin(idToken);
const user: ValidatedUser = { userId };

const permission: IUserPermissions = await this.userQryService.retrieveUserPermissions(userId);
const permission: UserPermissions = await this.userQryService.retrieveUserPermissions(userId);
if (permission && permission.legalEntityId) {
user.legalEntityId = permission.legalEntityId;
user.role = permission.role;
Expand Down
14 changes: 7 additions & 7 deletions src/command/api/data-stream.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { CommandBus } from '@nestjs/cqrs';
import { ApiBearerAuth, ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
import { v4 } from 'uuid';
import { ValidatedUser } from '../../auth/validated-user';
import { User } from '../../commons/decorators/user.decorator';
import { InjectUser } from '../../commons/decorators/user.decorator';
import { DomainExceptionFilter } from '../../commons/errors/domain-exception.filter';
import { Roles } from '../../commons/guards/roles.decorator';
import { RolesGuard } from '../../commons/guards/roles.guard';
import { UserRole } from '../../commons/user/user.schema';
import { UserRole } from '../../commons/user/user-permissions.schema';
import { AddDatastreamCommand } from '../model/data-stream/add-data-stream.command';
import { LinkObservationGoalCommand } from '../model/data-stream/link-observationgoal.command';
import { RemoveDatastreamCommand } from '../model/data-stream/remove-data-stream.command';
Expand All @@ -32,7 +32,7 @@ export class DatastreamController {
@ApiResponse({ status: 200, description: 'Datastream added' })
@ApiResponse({ status: 400, description: 'Datastream addition failed' })
async addDatastream(
@User() user: ValidatedUser,
@InjectUser() user: ValidatedUser,
@Param() params: SensorIdParams,
@Body() datastreamBody: AddDatastreamBody,
): Promise<any> {
Expand All @@ -54,7 +54,7 @@ export class DatastreamController {
@ApiResponse({ status: 200, description: 'Datastream updated' })
@ApiResponse({ status: 400, description: 'Datastream update failed' })
async updateDatastream(
@User() user: ValidatedUser,
@InjectUser() user: ValidatedUser,
@Param() params: DatastreamIdParams,
@Body() datastreamBody: UpdateDatastreamBody,
): Promise<any> {
Expand All @@ -72,7 +72,7 @@ export class DatastreamController {
@ApiResponse({ status: 200, description: 'Observation goal linked' })
@ApiResponse({ status: 400, description: 'Observation goal linking failed' })
async linkObservationGoal(
@User() user: ValidatedUser,
@InjectUser() user: ValidatedUser,
@Param() params: DatastreamIdParams,
@Body() observationGoalBody: ObservationGoalBody,
): Promise<any> {
Expand All @@ -93,7 +93,7 @@ export class DatastreamController {
@ApiResponse({ status: 200, description: 'Observation goal unlinked' })
@ApiResponse({ status: 400, description: 'Observation goal unlinking failed' })
async unlinkObservationGoal(
@User() user: ValidatedUser,
@InjectUser() user: ValidatedUser,
@Param() params: DatastreamIdParams,
@Body() observationGoalBody: ObservationGoalBody,
): Promise<any> {
Expand All @@ -114,7 +114,7 @@ export class DatastreamController {
@ApiOperation({ summary: 'Remove datastream' })
@ApiResponse({ status: 200, description: 'Datastream removed' })
@ApiResponse({ status: 400, description: 'Datastream removal failed' })
async removeDatastream(@User() user: ValidatedUser, @Param() params: DatastreamIdParams): Promise<any> {
async removeDatastream(@InjectUser() user: ValidatedUser, @Param() params: DatastreamIdParams): Promise<any> {
return this.commandBus.execute(
new RemoveDatastreamCommand(params.deviceId, params.sensorId, user.legalEntityId, params.datastreamId),
);
Expand Down
12 changes: 6 additions & 6 deletions src/command/api/device.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { CommandBus } from '@nestjs/cqrs';
import { ApiBearerAuth, ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
import { v4 } from 'uuid';
import { ValidatedUser } from '../../auth/validated-user';
import { User } from '../../commons/decorators/user.decorator';
import { InjectUser } from '../../commons/decorators/user.decorator';
import { DomainExceptionFilter } from '../../commons/errors/domain-exception.filter';
import { Roles } from '../../commons/guards/roles.decorator';
import { RolesGuard } from '../../commons/guards/roles.guard';
import { UserRole } from '../../commons/user/user.schema';
import { UserRole } from '../../commons/user/user-permissions.schema';
import { RegisterDeviceCommand } from '../model/device/register-device.command';
import { RelocateDeviceCommand } from '../model/device/relocate-device.command';
import { RemoveDeviceCommand } from '../model/device/remove-device.command';
Expand All @@ -30,7 +30,7 @@ export class DeviceController {
@ApiResponse({ status: 200, description: 'Device registered' })
@ApiResponse({ status: 400, description: 'Device registration failed' })
async registerDevice(
@User() user: ValidatedUser,
@InjectUser() user: ValidatedUser,
@Body() deviceBody: RegisterDeviceBody,
): Promise<Record<string, any>> {
const deviceId = v4();
Expand All @@ -46,7 +46,7 @@ export class DeviceController {
@ApiResponse({ status: 200, description: 'Device updated' })
@ApiResponse({ status: 400, description: 'Device update failed' })
async updateDevice(
@User() user: ValidatedUser,
@InjectUser() user: ValidatedUser,
@Param() params: DeviceIdParams,
@Body() deviceBody: UpdateDeviceBody,
): Promise<any> {
Expand All @@ -61,7 +61,7 @@ export class DeviceController {
@ApiResponse({ status: 200, description: 'Device relocated' })
@ApiResponse({ status: 400, description: 'Device relocation failed' })
async relocateDevice(
@User() user: ValidatedUser,
@InjectUser() user: ValidatedUser,
@Param() params: DeviceIdParams,
@Body() deviceBody: UpdateLocationBody,
): Promise<any> {
Expand All @@ -74,7 +74,7 @@ export class DeviceController {
@ApiOperation({ summary: 'Remove device' })
@ApiResponse({ status: 200, description: 'Device removed' })
@ApiResponse({ status: 400, description: 'Device removal failed' })
async removeDevice(@User() user: ValidatedUser, @Param() params: DeviceIdParams): Promise<any> {
async removeDevice(@InjectUser() user: ValidatedUser, @Param() params: DeviceIdParams): Promise<any> {
return this.commandBus.execute(new RemoveDeviceCommand(params.deviceId, user.legalEntityId));
}
}
14 changes: 7 additions & 7 deletions src/command/api/legal-entity.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { CommandBus } from '@nestjs/cqrs';
import { ApiBearerAuth, ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
import { v4 } from 'uuid';
import { ValidatedUser } from '../../auth/validated-user';
import { User } from '../../commons/decorators/user.decorator';
import { InjectUser } from '../../commons/decorators/user.decorator';
import { DomainExceptionFilter } from '../../commons/errors/domain-exception.filter';
import { Roles } from '../../commons/guards/roles.decorator';
import { RolesGuard } from '../../commons/guards/roles.guard';
import { UserRole } from '../../commons/user/user.schema';
import { UserRole } from '../../commons/user/user-permissions.schema';
import { AddPublicContactDetailsCommand } from '../model/legal-entity/add-public-contact-details.command';
import { RegisterOrganizationCommand } from '../model/legal-entity/register-organization.command';
import { RemoveContactDetailsCommand } from '../model/legal-entity/remove-contact-details.command';
Expand All @@ -32,7 +32,7 @@ export class LegalEntityController {
@ApiResponse({ status: 200, description: 'Organization registered' })
@ApiResponse({ status: 400, description: 'Organization registration failed' })
async registerOrganization(
@User() user: ValidatedUser,
@InjectUser() user: ValidatedUser,
@Body() registerOrganizationBody: RegisterOrganizationBody,
): Promise<Record<string, any>> {
const legalEntityId = v4();
Expand Down Expand Up @@ -68,7 +68,7 @@ export class LegalEntityController {
@ApiResponse({ status: 200, description: 'Organization updated' })
@ApiResponse({ status: 400, description: 'Organization update failed' })
async updateOrganization(
@User() user: ValidatedUser,
@InjectUser() user: ValidatedUser,
@Body() updateOrganizationBody: UpdateOrganizationBody,
): Promise<any> {
return this.commandBus.execute(
Expand All @@ -89,7 +89,7 @@ export class LegalEntityController {
@ApiResponse({ status: 200, description: 'Contact details updated' })
@ApiResponse({ status: 400, description: 'Contact details update failed' })
async updateContactDetails(
@User() user: ValidatedUser,
@InjectUser() user: ValidatedUser,
@Param() params: ContactDetailsParams,
@Body() updateContactDetailsBody: ContactDetailsBody,
): Promise<any> {
Expand All @@ -112,7 +112,7 @@ export class LegalEntityController {
@ApiOperation({ summary: 'Remove legal entity' })
@ApiResponse({ status: 200, description: 'Legal entity removed' })
@ApiResponse({ status: 400, description: 'Legal entity removal failed' })
async removeLegalEntity(@User() user: ValidatedUser): Promise<any> {
async removeLegalEntity(@InjectUser() user: ValidatedUser): Promise<any> {
return this.commandBus.execute(new RemoveLegalEntityCommand(user.legalEntityId));
}

Expand All @@ -136,7 +136,7 @@ export class LegalEntityController {
@ApiOperation({ summary: 'Remove contact details' })
@ApiResponse({ status: 200, description: 'Contact details removed' })
@ApiResponse({ status: 400, description: 'Contact details removal failed' })
async removeContactDetails(@User() user: ValidatedUser, @Param() params: ContactDetailsParams): Promise<any> {
async removeContactDetails(@InjectUser() user: ValidatedUser, @Param() params: ContactDetailsParams): Promise<any> {
return this.commandBus.execute(new RemoveContactDetailsCommand(user.legalEntityId, params.contactDetailsId));
}
}
10 changes: 5 additions & 5 deletions src/command/api/observation-goal.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { CommandBus } from '@nestjs/cqrs';
import { ApiBearerAuth, ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
import { v4 } from 'uuid';
import { ValidatedUser } from '../../auth/validated-user';
import { User } from '../../commons/decorators/user.decorator';
import { InjectUser } from '../../commons/decorators/user.decorator';
import { DomainExceptionFilter } from '../../commons/errors/domain-exception.filter';
import { Roles } from '../../commons/guards/roles.decorator';
import { RolesGuard } from '../../commons/guards/roles.guard';
import { UserRole } from '../../commons/user/user.schema';
import { UserRole } from '../../commons/user/user-permissions.schema';
import { RegisterObservationGoalCommand } from '../model/observation-goal/register-observation-goal.command';
import { RemoveObservationGoalCommand } from '../model/observation-goal/remove-observation-goal.command';
import { UpdateObservationGoalCommand } from '../model/observation-goal/update-observation-goal.command';
Expand All @@ -28,7 +28,7 @@ export class ObservationGoalController {
@ApiResponse({ status: 200, description: 'Observation goal registered' })
@ApiResponse({ status: 400, description: 'Observation goal registration failed' })
async registerObservationGoal(
@User() user: ValidatedUser,
@InjectUser() user: ValidatedUser,
@Body() observationGoalBody: RegisterObservationGoalBody,
): Promise<Record<string, any>> {
const observationGoalId = v4();
Expand All @@ -46,7 +46,7 @@ export class ObservationGoalController {
@ApiResponse({ status: 200, description: 'Observation goal updated' })
@ApiResponse({ status: 400, description: 'Observation goal update failed' })
async updateObservationGoal(
@User() user: ValidatedUser,
@InjectUser() user: ValidatedUser,
@Param() params: ObservationGoalIdParams,
@Body() observationGoalBody: UpdateObservationGoalBody,
): Promise<any> {
Expand All @@ -64,7 +64,7 @@ export class ObservationGoalController {
@ApiOperation({ summary: 'Remove observation goal' })
@ApiResponse({ status: 200, description: 'Observation goal removed' })
@ApiResponse({ status: 400, description: 'Observation goal removal failed' })
async removeObservationGoal(@User() user: ValidatedUser, @Param() params: ObservationGoalIdParams): Promise<any> {
async removeObservationGoal(@InjectUser() user: ValidatedUser, @Param() params: ObservationGoalIdParams): Promise<any> {
return this.commandBus.execute(new RemoveObservationGoalCommand(params.observationGoalId, user.legalEntityId));
}
}
10 changes: 5 additions & 5 deletions src/command/api/sensor.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { CommandBus } from '@nestjs/cqrs';
import { ApiTags, ApiResponse, ApiOperation, ApiBearerAuth } from '@nestjs/swagger';
import { v4 } from 'uuid';
import { ValidatedUser } from '../../auth/validated-user';
import { User } from '../../commons/decorators/user.decorator';
import { InjectUser } from '../../commons/decorators/user.decorator';
import { DomainExceptionFilter } from '../../commons/errors/domain-exception.filter';
import { Roles } from '../../commons/guards/roles.decorator';
import { RolesGuard } from '../../commons/guards/roles.guard';
import { UserRole } from '../../commons/user/user.schema';
import { UserRole } from '../../commons/user/user-permissions.schema';
import { AddSensorCommand } from '../model/sensor/add-sensor.command';
import { RemoveSensorCommand } from '../model/sensor/remove-sensor.command';
import { UpdateSensorCommand } from '../model/sensor/update-sensor.command';
Expand All @@ -29,7 +29,7 @@ export class SensorController {
@ApiResponse({ status: 200, description: 'Sensor registered' })
@ApiResponse({ status: 400, description: 'Sensor registration failed' })
async registerSensor(
@User() user: ValidatedUser,
@InjectUser() user: ValidatedUser,
@Param() params: DeviceIdParams,
@Body() sensorBody: AddSensorBody,
): Promise<Record<string, any>> {
Expand All @@ -48,7 +48,7 @@ export class SensorController {
@ApiResponse({ status: 200, description: 'Sensor updated' })
@ApiResponse({ status: 400, description: 'Sensor update failed' })
async updateSensor(
@User() user: ValidatedUser,
@InjectUser() user: ValidatedUser,
@Param() params: SensorIdParams,
@Body() sensorBody: UpdateSensorBody,
): Promise<any> {
Expand All @@ -63,7 +63,7 @@ export class SensorController {
@ApiOperation({ summary: 'Remove sensor' })
@ApiResponse({ status: 200, description: 'Sensor removed' })
@ApiResponse({ status: 400, description: 'Sensor removal failed' })
async removeSensor(@User() user: ValidatedUser, @Param() params: SensorIdParams): Promise<any> {
async removeSensor(@InjectUser() user: ValidatedUser, @Param() params: SensorIdParams): Promise<any> {
return this.commandBus.execute(new RemoveSensorCommand(params.sensorId, user.legalEntityId, params.deviceId));
}
}
Loading