|
1 |
| -# member-preferences-service |
| 1 | +# Member preferences service |
| 2 | + |
| 3 | +## Prerequisites |
| 4 | + |
| 5 | +- NodeJS (v10) |
| 6 | +- AWS DynamoDB |
| 7 | +- Java 6+ (if using runnable jar of local DynamoDB) |
| 8 | +- Docker, Docker Compose (if using docker of local DynamoDB) |
| 9 | + |
| 10 | +## Configuration |
| 11 | + |
| 12 | +Configuration for the application is at `config/default.js` and `config/production.js`. |
| 13 | +The following parameters can be set in config files or in env variables: |
| 14 | + |
| 15 | +- LOG_LEVEL: the log level |
| 16 | +- PORT: the server port |
| 17 | +- API_VERSION: the API version |
| 18 | +- AUTH_SECRET: TC Authentication secret |
| 19 | +- VALID_ISSUERS: valid issuers for TC authentication |
| 20 | +- AUTH0_URL: AUTH0 URL, used to get M2M token |
| 21 | +- AUTH0_PROXY_SERVER_URL: AUTH0 proxy server URL, used to get M2M token |
| 22 | +- AUTH0_AUDIENCE: AUTH0 audience, used to get M2M token |
| 23 | +- TOKEN_CACHE_TIME: AUTH0 token cache time, used to get M2M token |
| 24 | +- AUTH0_CLIENT_ID: AUTH0 client id, used to get M2M token |
| 25 | +- AUTH0_CLIENT_SECRET: AUTH0 client secret, used to get M2M token |
| 26 | +- AMAZON_AWS_REGION: the Amazon AWS region to access DynamoDB |
| 27 | +- AMAZON_AWS_DYNAMODB_READ_CAPACITY_UNITS: AWS DynamoDB read capacity units |
| 28 | +- AMAZON_AWS_DYNAMODB_WRITE_CAPACITY_UNITS: AWS DynamoDB write capacity units |
| 29 | +- AMAZON_AWS_DYNAMODB_ENDPOINT: DynamoDB endpoint, set it only for local DynamoDB |
| 30 | +- AMAZON_AWS_DYNAMODB_PREFERENCE_TABLE: AWS DynamoDB table for user preferences |
| 31 | +- MAILCHIMP_API_BASE_URL: Mailchimp API base URL |
| 32 | +- MAILCHIMP_API_KEY: Mailchimp API key |
| 33 | +- MAILCHIMP_LIST_ID: Mailchimp list/audience id |
| 34 | +- SEARCH_USERS_URL: URL to search users |
| 35 | + |
| 36 | +Set the following environment variables so that the app can get TC M2M token (use 'set' insted of 'export' for Windows OS): |
| 37 | + |
| 38 | +- export AUTH0_CLIENT_ID=8QovDh27SrDu1XSs68m21A1NBP8isvOt |
| 39 | +- export AUTH0_CLIENT_SECRET=3QVxxu20QnagdH-McWhVz0WfsQzA1F8taDdGDI4XphgpEYZPcMTF4lX3aeOIeCzh |
| 40 | +- export AUTH0_URL=https://topcoder-dev.auth0.com/oauth/token |
| 41 | +- export AUTH0_AUDIENCE=https://m2m.topcoder-dev.com/ |
| 42 | + |
| 43 | +## AWS Setup |
| 44 | + |
| 45 | +1. Download your AWS Credentials from AWS Console. Refer [AWS Documentation](https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/getting-your-credentials.html) |
| 46 | + |
| 47 | +2. Depending on your Operating System, create AWS credentials file in the path listed below |
| 48 | + |
| 49 | + ```bash |
| 50 | + Linux, Unix, and macOS users: ~/.aws/credentials |
| 51 | + |
| 52 | + Windows users: C:\Users\{USER_NAME}\.aws\credentials |
| 53 | + ``` |
| 54 | + |
| 55 | +3. credentials file should look like below |
| 56 | + |
| 57 | + ```bash |
| 58 | + [default] |
| 59 | + aws_access_key_id=SOME_ACCESS_KEY_ID |
| 60 | + aws_secret_access_key=SOME_SECRET_ACCESS_KEY |
| 61 | + ``` |
| 62 | + |
| 63 | +4. Configure AMAZON_AWS_REGION in config file or environment |
| 64 | + |
| 65 | +## Local DynamoDB setup (Optional) |
| 66 | + |
| 67 | +This page `https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBLocal.html` provides several ways to deploy local DynamoDB. |
| 68 | + |
| 69 | +If you want to use runnable jar of local DynamoDB: |
| 70 | + |
| 71 | +- see `https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBLocal.DownloadingAndRunning.html` for details |
| 72 | +- download the local DynamoDB of your region |
| 73 | +- extract out the downloaded archive |
| 74 | +- ensure Java 6+ is installed |
| 75 | +- in the extracted folder, run `java -Djava.library.path=./DynamoDBLocal_lib -jar DynamoDBLocal.jar -sharedDb` |
| 76 | +- configure the AMAZON_AWS_DYNAMODB_ENDPOINT config parameter to `http://localhost:8000` in config file or via environment variable |
| 77 | +- follow above section to configure AWS credential, when using local DynamoDB, any fake access key id and secret access key may be used |
| 78 | + |
| 79 | +If you want to use docker of local DynamoDB: |
| 80 | + |
| 81 | +- see `https://hub.docker.com/r/amazon/dynamodb-local` for details |
| 82 | +- you may go to `docker` folder, and run `docker-compose up` to start local DynamoDB |
| 83 | +- configure the AMAZON_AWS_DYNAMODB_ENDPOINT config parameter to `http://localhost:8000` in config file or via environment variable |
| 84 | +- follow above section to configure AWS credential, when using local DynamoDB, any fake access key id and secret access key may be used |
| 85 | + |
| 86 | +## AWS DynamoDB setup |
| 87 | + |
| 88 | +- setup AWS credential and region properly |
| 89 | +- do not configure the AMAZON_AWS_DYNAMODB_ENDPOINT config param, then AWS DynamoDB will be used by default |
| 90 | + |
| 91 | +## Mailchimp setup |
| 92 | + |
| 93 | +- register a new account at `https://mailchimp.com` |
| 94 | +- after login, see the URL of your admin page, it is like `https://us20.admin.mailchimp.com/`, |
| 95 | + here the `us20` is data center, it may be different for different users, |
| 96 | + the MAILCHIMP_API_BASE_URL config param is like `https://{data-center}.api.mailchimp.com/3.0`, |
| 97 | + so if data center is `us20`, then the MAILCHIMP_API_BASE_URL param should be `https://us20.api.mailchimp.com/3.0` |
| 98 | +- click right top profile -> Account -> Extras -> API keys, here you can generate API key, configure it to |
| 99 | + MAILCHIMP_API_KEY config param |
| 100 | +- click header Audience -> right side Manage Audience -> Settings, scroll down and you can see the audience id, |
| 101 | + configure it to MAILCHIMP_LIST_ID config param |
| 102 | +- click header Audience -> right side Manage Audience -> Settings -> Manage contacts -> Tags, here you can manage tags, |
| 103 | + create 3 tags: `Data Science Newsletter`, `Design Newsletter`, `Dev Newsletter` |
| 104 | + |
| 105 | +- during review, you may simply use the provided Mailchimp config params, and then you don't need to do above setup |
| 106 | +
|
| 107 | +## Local Deployment |
| 108 | +
|
| 109 | +- Install dependencies `npm install` |
| 110 | +- Run lint `npm run lint` |
| 111 | +- Run lint fix `npm run lint:fix` |
| 112 | +- To delete DynamoDB table if needed `npm run delete-table` |
| 113 | +- To create DynamoDB table if needed `npm run create-table` |
| 114 | +- Start app `npm start` |
| 115 | +- App is running at `http://localhost:3000` |
| 116 | +
|
| 117 | +## Verification |
| 118 | +
|
| 119 | +- import Postman collection and environment in the docs folder to Postman |
| 120 | +- then run the Postman tests |
| 121 | +
|
| 122 | +- you may use command `npm run view-table 23124329` to view DynamoDB preferences table record of given user id, |
| 123 | + console will show info like below: |
| 124 | +
|
| 125 | +```bash |
| 126 | +info: Data of id 23124329: { |
| 127 | + "id": "23124329", |
| 128 | + "email": { |
| 129 | + "firstName": "first name", |
| 130 | + "lastName": "last name", |
| 131 | + "subscriptions": { |
| 132 | + "Dev Newsletter": false, |
| 133 | + "Data Science Newsletter": true, |
| 134 | + "Design Newsletter": true |
| 135 | + }, |
| 136 | + "updatedBy": "user2", |
| 137 | + "createdBy": "user1" |
| 138 | + }, |
| 139 | + "objectId": "23124329", |
| 140 | + "updatedAt": "2019-04-19T18:24:15.128Z" |
| 141 | +} |
| 142 | +info: Done! |
| 143 | +``` |
| 144 | +
|
| 145 | +- if you are using AWS DynamoDB, you may also view the table content in AWS web console |
| 146 | +
|
| 147 | +## Notes |
| 148 | +
|
| 149 | +- swagger is at `docs/swagger.yaml`, you may check it using `http://editor.swagger.io/` |
| 150 | +- all JWT tokens provided in Postman environment file and tests are created in `https://jwt.io` with secret `mysecret` |
0 commit comments