This repository has been archived by the owner on Dec 24, 2022. It is now read-only.
generated from mrbrianevans/react-website-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add postprocessor and generator for contacts GH-23
#contacts
- Loading branch information
1 parent
106589f
commit 396a7c3
Showing
8 changed files
with
217 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
Family Name,Given Name,Group Membership,Organization 1 - Name,Organization 1 - Type,Phone 1 - Type,Phone 1 - Value,Name | ||
Turner,Matthew,* myContacts,,,Mobile,04841 794049,Matthew Turner | ||
Lewis,Brian,* myContacts,,,Mobile,02794 678454,Brian Lewis | ||
Taylor,Michael,* myContacts,,,Mobile,05380 178879,Michael Taylor | ||
Smith,Alan,* myContacts,,,Mobile,04623 971739,Alan Smith | ||
John Robinson,David,* myContacts,,,Mobile,03749 234774,David John Robinson | ||
Morris,Andrew,* myContacts,,,Mobile,08458 206820,Andrew Morris | ||
Hill,Brian,* myContacts,,,Mobile,04874 833649,Brian Hill | ||
Ward,Paul,* myContacts,,,Mobile,09730 103706,Paul Ward | ||
Taylor,John,* myContacts,,,Mobile,06671 694507,John Taylor | ||
Scott,Mark,* myContacts,,,Mobile,08304 579657,Mark Scott |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { RandInt } from './RandomNumberUtils' | ||
|
||
export function generateRandomPhoneNumber() { | ||
const firstPart = '0' + RandInt(4).toString() | ||
const secondPart = RandInt(6).toString() | ||
return firstPart + ' ' + secondPart | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,21 @@ | ||
import { PreProcessor } from '../../typedefs/PreProcess' | ||
import * as Papa from 'papaparse' | ||
export const csvPreProcessor: PreProcessor< | ||
{ [key: string]: string | number | boolean }[] | ||
> = ({ filename, fileType, fileContent }) => { | ||
const data = readCsvStringToObject(fileContent) | ||
|
||
export type CsvObject = { [key: string]: string | number | boolean }[] | ||
export const csvPreProcessor: PreProcessor<CsvObject> = ({ | ||
filename, | ||
fileType, | ||
fileContent | ||
}) => { | ||
const { data, meta } = Papa.parse(fileContent, { | ||
header: true, | ||
skipEmptyLines: true, | ||
dynamicTyping: true | ||
}) | ||
const { length } = data | ||
return { | ||
data, | ||
title: filename, | ||
metadata: { length } | ||
metadata: { length, ...meta } | ||
} | ||
} | ||
|
||
const readCsvStringToObject = (csvString) => { | ||
return Papa.parse(csvString, { | ||
header: true, | ||
skipEmptyLines: true, | ||
dynamicTyping: true | ||
}).data | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
import { PostProcessor } from '../../typedefs/PostProcess' | ||
import * as Papa from 'papaparse' | ||
import { getRandomFullName } from '../../common/RandomUtils/RandomContent/RandomNames' | ||
import { generateRandomPhoneNumber } from '../../common/RandomUtils/RandomPhoneNumber' | ||
export type CsvContacts = CsvContactsChild[] | ||
export interface CsvContactsChild { | ||
Name: string | ||
'Given Name': string | ||
'Additional Name'?: string | ||
'Family Name': string | ||
'Yomi Name'?: string | ||
'Given Name Yomi'?: string | ||
'Additional Name Yomi'?: string | ||
'Family Name Yomi'?: string | ||
'Name Prefix'?: string | ||
'Name Suffix'?: string | ||
Initials?: string | ||
Nickname?: string | ||
'Short Name'?: string | ||
'Maiden Name'?: string | ||
Birthday?: string | ||
Gender?: string | ||
Location?: string | ||
'Billing Information'?: string | ||
'Directory Server'?: string | ||
Mileage?: string | ||
Occupation?: string | ||
Hobby?: string | ||
Sensitivity?: string | ||
Priority?: string | ||
Subject?: string | ||
Notes?: string | ||
Language?: string | ||
Photo?: string | ||
'Group Membership': string | ||
'E-mail 1 - Type'?: string | ||
'E-mail 1 - Value'?: string | ||
'E-mail 2 - Type'?: string | ||
'E-mail 2 - Value'?: string | ||
'E-mail 3 - Type'?: string | ||
'E-mail 3 - Value'?: string | ||
'IM 1 - Type'?: string | ||
'IM 1 - Service'?: string | ||
'IM 1 - Value'?: string | ||
'Phone 1 - Type': string | ||
'Phone 1 - Value': string | ||
'Phone 2 - Type'?: string | ||
'Phone 2 - Value'?: string | ||
'Address 1 - Type'?: string | ||
'Address 1 - Formatted'?: string | ||
'Address 1 - Street'?: string | ||
'Address 1 - City'?: string | ||
'Address 1 - PO Box'?: string | ||
'Address 1 - Region'?: string | ||
'Address 1 - Postal Code'?: string | ||
'Address 1 - Country'?: string | ||
'Address 1 - Extended Address'?: string | ||
'Organization 1 - Type': string | ||
'Organization 1 - Name': string | ||
'Organization 1 - Yomi Name'?: string | ||
'Organization 1 - Title'?: string | ||
'Organization 1 - Department'?: string | ||
'Organization 1 - Symbol'?: string | ||
'Organization 1 - Location'?: string | ||
'Organization 1 - Job Description'?: string | ||
'Website 1 - Type'?: string | ||
'Website 1 - Value'?: string | ||
'Custom Field 1 - Type'?: string | ||
'Custom Field 1 - Value'?: string | ||
} | ||
export interface Contact { | ||
fullName: string | ||
profilePictureUrl?: string | ||
firstName: string | ||
lastName: string | ||
phoneNumbers: { phoneNumber: string; label: string }[] | ||
emailAddresses: { emailAddress: string; label: string }[] | ||
organisation?: string | ||
address?: { | ||
streetAddress: string | ||
postCode: string | ||
} | ||
otherDetails: Record<string, any> | ||
} | ||
|
||
export const processContacts: PostProcessor<CsvContacts, Contact[]> = ({ | ||
preProcessedOutput: { data, metadata } | ||
}) => { | ||
const { fields } = metadata | ||
console.log(data) | ||
const contacts: Contact[] = [] | ||
for (const contact of data) { | ||
contacts.push({ | ||
address: { | ||
postCode: contact['Address 1 - Postal Code'], | ||
streetAddress: contact['Address 1 - Street'] | ||
}, | ||
emailAddresses: [ | ||
{ | ||
emailAddress: contact['E-mail 1 - Value'], | ||
label: contact['E-mail 1 - Type'] | ||
} | ||
], | ||
firstName: contact['Given Name'], | ||
fullName: contact.Name, | ||
lastName: contact['Family Name'], | ||
organisation: contact['Organization 1 - Name'], | ||
otherDetails: contact, | ||
phoneNumbers: [ | ||
{ | ||
phoneNumber: contact['Phone 1 - Value'], | ||
label: contact['Phone 1 - Type'] | ||
} | ||
], | ||
profilePictureUrl: contact.Photo | ||
}) | ||
} | ||
return { | ||
data: contacts, | ||
title: 'Contacts', | ||
metadata | ||
} | ||
} | ||
|
||
function generateContacts(qty = 10): CsvContacts { | ||
const contacts = new Set<CsvContacts[number]>() | ||
for (let i = 0; i < qty; i++) { | ||
const Name = getRandomFullName() | ||
contacts.add({ | ||
'Family Name': Name.split(' ').slice(1).join(' '), | ||
'Given Name': Name.split(' ').slice(0, 1).join(' '), | ||
'Group Membership': '* myContacts', | ||
'Organization 1 - Name': '', | ||
'Organization 1 - Type': '', // can be 'unknown' | ||
'Phone 1 - Type': 'Mobile', | ||
'Phone 1 - Value': generateRandomPhoneNumber(), | ||
Name | ||
}) | ||
} | ||
return Array.from(contacts) | ||
} | ||
|
||
export function generateContactsFile(qty = 10): string { | ||
const contacts = generateContacts(qty) | ||
return Papa.unparse(contacts) | ||
} |