Skip to content

Commit

Permalink
dni value added to the GET dashboard/users enpoint response (#1179)
Browse files Browse the repository at this point in the history
* dni value added to the GET `dashboard/users` enpoint response

* version changed

* dniQueryStringSchema added

* Update services/sso/src/__tests__/dashboard/users/list.test.ts

Co-authored-by: Danny Mejía <113645971+xpan1c@users.noreply.github.com>

---------

Co-authored-by: Danny Mejía <113645971+xpan1c@users.noreply.github.com>
  • Loading branch information
MiguelKummetz and danny-mv authored Apr 18, 2024
1 parent 7d1c736 commit 83f4601
Show file tree
Hide file tree
Showing 10 changed files with 105 additions and 19 deletions.
6 changes: 6 additions & 0 deletions services/sso/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
All notable changes to this project will be documented in this file.
# Changelog

## [1.21.0] - 2024-04-18

### Changed

- dni value added to the GET `dashboard/users` enpoint response.

## [1.20.0] - 2024-04-17

### Added
Expand Down
53 changes: 52 additions & 1 deletion services/sso/openapi.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
info:
version: 1.20.0
version: 1.21.0
title: IT Academy SSO Service
description: This is an SSO service that is used across all ITA services.
openapi: 3.0.0
Expand Down Expand Up @@ -673,6 +673,14 @@ paths:
example: John Doe
required: false
description: Partial or full name to filter the users by
- in: query
name: dni
schema:
type: string
minLength: 2
maxLength: 9
required: false
description: Partial or full dni to filter the users by
responses:
"200":
description: Token is valid and user information is returned.
Expand All @@ -685,6 +693,8 @@ paths:
properties:
id:
type: string
dni:
type: string
name:
type: string
minLength: 1
Expand All @@ -703,6 +713,7 @@ paths:
example: Frontend Angular
required:
- id
- dni
- name
- status
- createdAt
Expand Down Expand Up @@ -837,6 +848,46 @@ paths:
schema:
type: string
required: true
requestBody:
required: true
description: Updates an existing user. The ID is mandatory, and all other fields
are optional. Provide only the fields that need to be updated.
content:
application/json:
schema:
type: object
properties:
dni:
type: string
email:
type: string
minLength: 1
format: email
example: user@example.cat
name:
type: string
minLength: 1
example: John Doe
password:
type: string
minLength: 8
role:
type: string
enum:
- ADMIN
- MENTOR
- REGISTERED
status:
type: string
enum:
- ACTIVE
- PENDING
- BLOCKED
deletedAt:
type: string
format: date-time
itineraryId:
type: string
responses:
"204":
description: User has been updated
Expand Down
4 changes: 2 additions & 2 deletions services/sso/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion services/sso/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sso-service",
"version": "1.20.0",
"version": "1.21.0",
"description": "",
"directories": {
"test": "test"
Expand Down
19 changes: 16 additions & 3 deletions services/sso/src/__tests__/dashboard/users/list.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { UserStatus } from '../../../schemas/users/userSchema'
const route = `${pathRoot.v1.dashboard.users}`

const responseSchema = userSchema
.pick({ id: true, name: true, status: true })
.pick({ id: true, name: true, dni: true, status: true })
.extend({
createdAt: z.string().regex(/^\d{4}-\d{2}-\d{2}$/),
itineraryName: z.string(),
Expand All @@ -32,9 +32,10 @@ beforeAll(async () => {
`SELECT
u.id,
u.name AS name,
i.name AS "itineraryName",
u.dni AS dni,
u.status,
TO_CHAR(u.created_at, 'YYYY-MM-DD') AS "createdAt"
TO_CHAR(u.created_at, 'YYYY-MM-DD') AS "createdAt",
i.name AS "itineraryName"
FROM
"user" u
JOIN itinerary i ON u.itinerary_id = i.id;`
Expand Down Expand Up @@ -205,4 +206,16 @@ describe('Testing get users endpoint', () => {
expect(response.status).toBe(401)
expect(response.body.message).toBe('Invalid Credentials')
})
it('returns a user successfully with a dni query of 2 or more characters', async () => {
const validDni = 'Z45035'
const response = await supertest(server)
.get(route)
.query({ dni: validDni })
.set('Cookie', [authAdminToken])
const { body }: { body: DashboardUsersList } = response
expect(response.status).toBe(200)
expect(body).toBeInstanceOf(Array)
expect(body).toHaveLength(1)
expect(responseSchema.safeParse(body).success).toBeTruthy()
})
})
30 changes: 19 additions & 11 deletions services/sso/src/controllers/dashboard/users/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,34 @@ import {
endDateSchema,
startDateSchema,
} from '../../../schemas/users/dashboardUsersListQuerySchema'
import { dniQueryStringSchema } from '../../../schemas/dniQueryStringSchema'

export const dashboardListUsers: Middleware = async (ctx: Context) => {
const { itinerarySlug, status, startDate, endDate, name } = ctx.state.query
const { itinerarySlug, status, startDate, endDate, name, dni } =
ctx.state.query
let query = `
SELECT
u.id,
u.name AS name,
i.name AS "itineraryName",
u.dni AS dni,
u.status,
TO_CHAR(u.created_at, 'YYYY-MM-DD') AS "createdAt"
TO_CHAR(u.created_at, 'YYYY-MM-DD') AS "createdAt",
i.name AS "itineraryName"
FROM
"user" u
JOIN itinerary i ON u.itinerary_id = i.id
`
const queryParams = []
const conditions = []
if (itinerarySlug) {
const parsedSlug = itinerarySlugSchema.parse(itinerarySlug)
conditions.push(`i.slug = $${conditions.length + 1}`)
queryParams.push(parsedSlug)
if (name) {
const parsedName = userNameSchema.parse(name)
conditions.push(`u.name ILIKE $${conditions.length + 1}`)
queryParams.push(`%${parsedName}%`)
}
if (dni) {
const parsedDni = dniQueryStringSchema.parse(dni)
conditions.push(`u.dni ILIKE $${conditions.length + 1}`)
queryParams.push(`%${parsedDni}%`)
}
if (status) {
const parsedStatus = userStatusSchema.parse(status)
Expand All @@ -45,10 +53,10 @@ export const dashboardListUsers: Middleware = async (ctx: Context) => {
conditions.push(`u.created_at <= $${conditions.length + 1}`)
queryParams.push(parsedEndDate)
}
if (name) {
const parsedName = userNameSchema.parse(name)
conditions.push(`u.name ILIKE $${conditions.length + 1}`)
queryParams.push(`%${parsedName}%`)
if (itinerarySlug) {
const parsedSlug = itinerarySlugSchema.parse(itinerarySlug)
conditions.push(`i.slug = $${conditions.length + 1}`)
queryParams.push(parsedSlug)
}
if (conditions.length > 0) {
query += ` WHERE ${conditions.join(' AND ')}`
Expand Down
2 changes: 1 addition & 1 deletion services/sso/src/openapi/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export const openapiFilename = 'openapi.yaml'

export const generatorConfig = {
info: {
version: '1.20.0',
version: '1.21.0',
title: 'IT Academy SSO Service',
description: 'This is an SSO service that is used across all ITA services.',
},
Expand Down
3 changes: 3 additions & 0 deletions services/sso/src/schemas/dniQueryStringSchema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { z } from '../openapi/zod'

export const dniQueryStringSchema = z.string().min(2).max(9)
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { z } from '../../openapi/zod'
import { dniQueryStringSchema } from '../dniQueryStringSchema'
import { itinerarySlugSchema } from '../itineraries/itinerarySchema'
import { UserStatus, userNameSchema, userStatusSchema } from './userSchema'

Expand Down Expand Up @@ -39,4 +40,7 @@ export const dashboardUsersListQuerySchema = z.object({
.openapi({
param: { description: 'Partial or full name to filter the users by' },
}),
dni: dniQueryStringSchema.optional().openapi({
param: { description: 'Partial or full dni to filter the users by' },
}),
})
1 change: 1 addition & 0 deletions services/sso/src/schemas/users/dashboardUsersListSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { z } from '../../openapi/zod'
export const dashboardUsersListSchema = userSchema
.pick({
id: true,
dni: true,
name: true,
status: true,
})
Expand Down

0 comments on commit 83f4601

Please sign in to comment.