Skip to content

Commit

Permalink
Refactor localities endpoint to improve response structure and update…
Browse files Browse the repository at this point in the history
… locality name in schema
  • Loading branch information
joao-vasconcelos committed Jan 22, 2025
1 parent b28ff84 commit 04fd2d7
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
1 change: 0 additions & 1 deletion apps/server/src/endpoints/locations/districts.endpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,4 @@ const handler = async (_: FastifyRequest, reply: FastifyReply) => {

/* * */

// FASTIFY.GET('/locations/districts', handler);
FASTIFY.GET('/locations/districts', handler, { schema });
38 changes: 32 additions & 6 deletions apps/server/src/endpoints/locations/localities.endpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,54 @@ import type { FastifyReply, FastifyRequest } from 'fastify';
import { FASTIFY } from '@/services/FASTIFY.js';
import { SERVERDB } from '@carrismetropolitana/api-services';
import { SERVERDB_KEYS } from '@carrismetropolitana/api-settings';
import { LocalitySchema } from '@carrismetropolitana/api-types/locations';
import { ApiResponse, ApiResponseErrorSchema, ApiResponseSuccessSchema } from '@carrismetropolitana/api-types/common';
import { Locality, LocalitySchema } from '@carrismetropolitana/api-types/locations';
import fastify from 'fastify';
import { createSchema } from 'zod-openapi';

/* * */

const schema = {
const schema: fastify.RouteShorthandOptions['schema'] = {
description: 'Get all Localities',
response: {
200: LocalitySchema.array(),
200: {
content: {
'application/json': {
schema: createSchema(ApiResponseSuccessSchema(LocalitySchema.array())).schema,
},
},
description: '200 OK',
},
500: {
content: {
'application/json': {
schema: createSchema(ApiResponseErrorSchema).schema,
},
},
description: '500 Internal Server Error',
},
},
summary: 'Get all ENCM facilities',
summary: 'Get all Localities',
tags: ['locations'],
};

/* * */

const handler = async (_: FastifyRequest, reply: FastifyReply) => {
//

const allItemsTxt = await SERVERDB.get(SERVERDB_KEYS.LOCATIONS.LOCALITIES);
if (!allItemsTxt) return reply.code(404).send([]);

const response: ApiResponse<Locality[]> = {
data: JSON.parse(allItemsTxt) || [],
status: 'success',
timestamp: Date.now(),
};

return reply
.code(200)
.header('cache-control', 'public, max-age=3600')
.send(allItemsTxt);
.send(JSON.stringify(response));
};

/* * */
Expand Down
2 changes: 1 addition & 1 deletion packages/types/src/api/locations/localities.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const LocalitySchema = z
district_id: '01',
id: '010101-1234',
municipality_id: '0101',
name: 'Locality 01',
name: 'Locality 1234',
parish_id: '010101',
},
title: 'Locality',
Expand Down

0 comments on commit 04fd2d7

Please sign in to comment.