Skip to content
Merged
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
8 changes: 8 additions & 0 deletions apps/api/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -10413,6 +10413,14 @@
"type": "string",
"description": "Nuevo horario. Cadena vacía lo borra. Omitir para no cambiarlo.",
"nullable": true
},
"location": {
"description": "Nueva ubicación (dirección + coordenadas) para corregir la geolocalización. Omitir para no cambiarla.",
"allOf": [
{
"$ref": "#/components/schemas/LocationDto"
}
]
}
},
"required": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,30 @@ describe('EditResource', () => {
]);
});

it('corrects the location in place and reports the coordinate diff', async () => {
const id = await seed();

const result = await editResource.execute({
resourceId: id,
location: { address: 'Av. Bolívar 5', latitude: 10.5, longitude: -66.92 },
});

const resource = await repo.findById(ResourceId.fromString(id));
expect(resource!.location.toPlain()).toEqual({
address: 'Av. Bolívar 5',
latitude: 10.5,
longitude: -66.92,
});
expect(result.changes).toEqual(
expect.arrayContaining([
{ field: 'address', before: 'Caracas', after: 'Av. Bolívar 5' },
{ field: 'latitude', before: 10.48, after: 10.5 },
{ field: 'longitude', before: -66.9, after: -66.92 },
]),
);
expect(result.changes).toHaveLength(3);
});

it('throws ResourceNotFoundError for an unknown id', async () => {
await expect(
editResource.execute({ resourceId: OWNER, name: 'x' }),
Expand Down
10 changes: 10 additions & 0 deletions apps/api/src/contexts/resources/application/edit-resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ResourceRepository } from '../domain/ports/resource.repository';
import { ResourceId } from '../domain/resource-id';
import { EditResourceProps } from '../domain/resource';
import { ResourceNotFoundError } from './resource-not-found.error';
import { Location, LocationProps } from '../../../shared/domain/location';
import {
MutationAuditResult,
diffFields,
Expand All @@ -13,6 +14,7 @@ export interface EditResourceCommand {
description?: string | null;
contact?: string | null;
schedule?: string | null;
location?: LocationProps;
}

/**
Expand All @@ -34,20 +36,28 @@ export class EditResource {
description: resource.description,
contact: resource.contact,
schedule: resource.schedule,
address: resource.location.toPlain().address,
latitude: resource.location.toPlain().latitude,
longitude: resource.location.toPlain().longitude,
};

const edit: EditResourceProps = {};
if (cmd.name !== undefined) edit.name = cmd.name;
if (cmd.description !== undefined) edit.description = cmd.description;
if (cmd.contact !== undefined) edit.contact = cmd.contact;
if (cmd.schedule !== undefined) edit.schedule = cmd.schedule;
if (cmd.location !== undefined)
edit.location = Location.create(cmd.location);
resource.edit(edit);

const after = {
name: resource.name,
description: resource.description,
contact: resource.contact,
schedule: resource.schedule,
address: resource.location.toPlain().address,
latitude: resource.location.toPlain().latitude,
longitude: resource.location.toPlain().longitude,
};

await this.repo.save(resource);
Expand Down
10 changes: 9 additions & 1 deletion apps/api/src/contexts/resources/domain/resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ export interface EditResourceProps {
description?: string | null;
contact?: string | null;
schedule?: string | null;
/** Correct the point's geolocation (address + coordinates) in place (#—). */
location?: Location;
}

// Snapshot used by repositories to rehydrate without going through register().
Expand Down Expand Up @@ -117,7 +119,7 @@ export class Resource {
public readonly type: ResourceType,
private _name: string,
private _description: string | null,
public readonly location: Location,
private _location: Location,
public readonly ownerUserId: string,
public readonly ownerOrganizationId: string | null,
private _verificationLevel: VerificationLevel,
Expand Down Expand Up @@ -213,6 +215,9 @@ export class Resource {
get description(): string | null {
return this._description;
}
get location(): Location {
return this._location;
}
get contact(): string | null {
return this._contact;
}
Expand Down Expand Up @@ -316,6 +321,9 @@ export class Resource {
this._schedule =
props.schedule === null ? null : props.schedule.trim() || null;
}
if (props.location !== undefined) {
this._location = props.location;
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,10 @@ export class DrizzleResourceRepository implements ResourceRepository {
verificationLevel: s.verificationLevel,
publicStatus: s.publicStatus,
name: s.name,
description: s.description,
address: s.location.address,
latitude: s.location.latitude,
longitude: s.location.longitude,
contact: s.contact,
schedule: s.schedule,
manager: s.manager,
Expand Down
10 changes: 10 additions & 0 deletions apps/api/src/contexts/resources/infrastructure/http/dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,16 @@ export class EditResourceDto {
@IsOptional()
@IsString()
schedule?: string;

@ApiPropertyOptional({
description:
'Nueva ubicación (dirección + coordenadas) para corregir la geolocalización. Omitir para no cambiarla.',
type: LocationDto,
})
@IsOptional()
@ValidateNested()
@Type(() => LocationDto)
location?: LocationDto;
}

export class ReportResourceValidityDto {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ export class ResourcesController {
if (dto.description !== undefined) cmd.description = dto.description;
if (dto.contact !== undefined) cmd.contact = dto.contact;
if (dto.schedule !== undefined) cmd.schedule = dto.schedule;
if (dto.location !== undefined) cmd.location = dto.location;

const result = await this.editResource.execute(cmd);
setAuditContext(req, {
Expand Down
2 changes: 2 additions & 0 deletions packages/api-client/src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3320,6 +3320,8 @@ export interface components {
contact?: string | null;
/** @description Nuevo horario. Cadena vacía lo borra. Omitir para no cambiarlo. */
schedule?: string | null;
/** @description Nueva ubicación (dirección + coordenadas) para corregir la geolocalización. Omitir para no cambiarla. */
location?: components["schemas"]["LocationDto"];
};
DiscardResourceDto: {
/**
Expand Down
Loading