Skip to content

Commit

Permalink
Support the creation of contacts
Browse files Browse the repository at this point in the history
Add a POST endpoint to the contacts controller to access the contact
creation service

closes #1247

Co-authored-by: horatio <[email protected]>
  • Loading branch information
TangoYankee and horatiorosa committed Oct 18, 2024
1 parent 6371b25 commit dcd5e9c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
12 changes: 10 additions & 2 deletions server/src/contact/contact.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
UsePipes,
Body,
Param,
Post,
} from '@nestjs/common';
import { ContactService } from './contact.service';
import { JsonApiSerializeInterceptor } from '../json-api-serialize.interceptor';
Expand Down Expand Up @@ -41,7 +42,7 @@ export class ContactController {
) {}

@Get('/')
async getContact(@Session() session, @Query('me') me, @Query('email') email) {
async getContact(@Session() session, @Query('me') me, @Query('email') email, @Query('includeAllStatusCodes') includeAllStatusCodes = 'false') {
// if this is a self-check, lookup the contact id from session
if (me) {
const { contactId } = session;
Expand All @@ -55,10 +56,17 @@ export class ContactController {
return this.contactService.findOneById(contactId);
}
} else {
return this.contactService.findOneByEmail(email);
return this.contactService.findOneByEmail(email, includeAllStatusCodes === 'true');
}
}

@UseGuards(AuthenticateGuard)
@UsePipes(JsonApiDeserializePipe)
@Post('/')
async create(@Body() body) {
return await this.contactService.create(body);
}

@UseGuards(AuthenticateGuard)
@UsePipes(JsonApiDeserializePipe)
@Patch('/:id')
Expand Down
5 changes: 3 additions & 2 deletions server/src/contact/contact.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,10 @@ export class ContactService {
* Uses the CRM Web API to query and return a Contact entity for given email
*
* @param {string} email Email matching a CRM Contact Entity's emailaddress1 property
* @param {boolean} includeAllStatusCodes A boolean on whether to only return active contacts. defaults to false.
* @return {object} Object representing a CRM contact
*/
public async findOneByEmail(email: string) {
public async findOneByEmail(email: string, includeAllStatusCodes = false) {
try {
const {
records: [firstRecord = GHOST_CONTACT],
Expand All @@ -88,7 +89,7 @@ export class ContactService {
`
$select=${CONTACT_ATTRS.join(',')}
&$filter=startswith(emailaddress1, '${email}')
and statuscode eq ${ACTIVE_CODE}
${includeAllStatusCodes ? '' : `and statuscode eq ${ACTIVE_CODE}`}
&$top=1
`,
);
Expand Down

0 comments on commit dcd5e9c

Please sign in to comment.