Skip to content

Commit

Permalink
Add forms/:formId/event endpoint (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mitelak committed Mar 5, 2020
1 parent 7c85838 commit c09c98f
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/api/EventsApi.ts
Expand Up @@ -82,4 +82,4 @@ function _handleFile(request: AuthRequest): Promise<any> {
resolve()
})
})
}
}
2 changes: 1 addition & 1 deletion src/api/FormSchemaApi.ts
Expand Up @@ -41,4 +41,4 @@ export class FormSchemaApi extends Controller {
return await FormSchemaService.getPrivate(id)
}

}
}
18 changes: 18 additions & 0 deletions src/api/FormsApi.ts
@@ -0,0 +1,18 @@
import {Controller, Get, Route, Tags} from "tsoa"
import Response from "common/Response"
import {EventPublicDoc} from "models/Event"
import EventService from "service/EventService"

@Route()
@Tags('Forms')
export class FormsApi extends Controller {

/**
* Get public event by form id
* @param formId form id
*/
@Get('/forms/{formId}/event')
public async getPublicEvent(formId: string): Promise<Response<EventPublicDoc>> {
return await EventService.findPublicByFormId(formId)
}
}
2 changes: 1 addition & 1 deletion src/api/ParticipantApi.ts
Expand Up @@ -94,4 +94,4 @@ export class ParticipantApi extends Controller {
return await ParticipiantService.remove(id, participantId, request.user)
}

}
}
13 changes: 12 additions & 1 deletion src/common/utils.ts
@@ -1,3 +1,6 @@
import { EventModel } from "models/Event"
import Exception from "./Exception"

export function isObjectID(text: string): boolean {
const regex = new RegExp('^[0-9a-fA-F]{24}$')
return regex.test(text)
Expand All @@ -7,4 +10,12 @@ export function byIdQuery(id: string): {_id: string} | {slug: string} {
return isObjectID(id)
? { _id: id }
: { slug: id }
}
}

export async function getEventIdFromFormId(formId: string): Promise<string> {
const result = await EventModel.findOne({
'forms': formId
})
if(!result) throw Exception.fromMessage(`Nie ma wydarzenia powiązanego z podanym formularzem: ${formId}`)
return result._id
}
17 changes: 12 additions & 5 deletions src/models/Event.ts
Expand Up @@ -15,15 +15,22 @@ export interface EventRequest {
usersEmails: string[];
}

export interface EventDoc extends Document{
administrators: AdministratorDoc[];
forms: string[];
export interface EventPublicDoc extends Document{
name: string;
emailAlias: string;
startDate: string;
endDate: string;
logo: string;
slug: string;
}

export interface EventDoc extends Document{
administrators: AdministratorDoc[];
forms: string[];
name: string;
emailAlias: string;
startDate: string;
endDate: string;
logo: string;
slug: string;
}

const EventSchema = new Schema(
Expand Down
16 changes: 12 additions & 4 deletions src/service/EventService.ts
@@ -1,7 +1,7 @@
import fs from 'fs'
import {EventDoc, EventModel, EventRequest, EventUpdateRequest} from 'models/Event'
import {EventDoc, EventModel, EventRequest, EventUpdateRequest, EventPublicDoc} from 'models/Event'
import Response from 'common/Response'
import {byIdQuery} from 'common/utils'
import {byIdQuery, getEventIdFromFormId} from 'common/utils'
import UserService from './AdministratorService'
import {logger} from 'common/logger'
import {Administrator} from 'models/Administrator'
Expand Down Expand Up @@ -92,10 +92,18 @@ async function findById(id: string): Promise<Response<EventDoc>> {
return new Response(result)
}

async function findPublicByFormId(formId: string): Promise<Response<EventPublicDoc>> {
logger.info(`Fetching public event data for ${formId} form`)
const query = await getEventIdFromFormId(formId)
const result = await EventModel.findOne(query, 'name startDate endDate logo')
return new Response(result)
}

export default {
add,
remove,
update,
findAll,
findById
}
findById,
findPublicByFormId,
}
15 changes: 4 additions & 11 deletions src/service/ParticipiantService.ts
Expand Up @@ -8,6 +8,7 @@ import {Query} from 'models/Query'
import clog, {CHANGE_TYPE} from 'service/ChangesLogerService'
import {EventModel} from 'models/Event'
import {TokenPayload} from 'google-auth-library'
import {getEventIdFromFormId} from 'common/utils'

export enum ACCESS_TYPE {
PRIVATE = 'private',
Expand Down Expand Up @@ -56,7 +57,7 @@ async function add(formSlug: string, type: ACCESS_TYPE, data: Participiant): Pro

async function edit(formSlug: string, query: Record<string, string>, data: Record<string, string>, user: TokenPayload): Promise<Response<ParticipantDoc[]>> {
const formModel = await _getModel(formSlug, ACCESS_TYPE.PRIVATE)
const eventId = await _getEventIdFromFormId(formSlug)
const eventId = await getEventIdFromFormId(formSlug)
return clog.methodWithMultipleChangelog(
formModel,
eventId,
Expand All @@ -77,7 +78,7 @@ async function edit(formSlug: string, query: Record<string, string>, data: Recor

async function editOne(formId: string, participantId: string, data: Record<string, string>, user: TokenPayload): Promise<Response<Participiant>> {
const formModel = await _getModel(formId, ACCESS_TYPE.PRIVATE)
const eventId = await _getEventIdFromFormId(formId)
const eventId = await getEventIdFromFormId(formId)
return clog.methodWithSingleChangelog(
formModel,
eventId,
Expand All @@ -94,7 +95,7 @@ async function editOne(formId: string, participantId: string, data: Record<strin

async function remove(formId: string, participantId: string, user: TokenPayload): Promise<Response<Participiant>> {
const formModel = await _getModel(formId, ACCESS_TYPE.PRIVATE)
const eventId = await _getEventIdFromFormId(formId)
const eventId = await getEventIdFromFormId(formId)
return clog.methodWithSingleChangelog(
formModel,
eventId,
Expand Down Expand Up @@ -168,14 +169,6 @@ function _prepareFilters(query: Query): { [p: string]: { $in: string[] } | { $re
}), {})
}

async function _getEventIdFromFormId(formId: string): Promise<string> {
const result = await EventModel.findOne({
'forms': formId
})
if(!result) throw Exception.fromMessage(`Nie ma wydarzenia powiązanego z podanym formularzem: ${formId}`)
return result._id
}

export default {
add,
remove,
Expand Down

0 comments on commit c09c98f

Please sign in to comment.