|
| 1 | +/** |
| 2 | + * Copyright 2018, Google, LLC. |
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. |
| 5 | + * You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +'use strict'; |
| 17 | + |
| 18 | +const basicCompanySample = require(`./basic-company-sample`); |
| 19 | +const basicJobSample = require(`./basic-job-sample`); |
| 20 | +const createAuthCredential = require('./create-auth-credential'); |
| 21 | + |
| 22 | +const PROJECT_ID = process.env.GOOGLE_CLOUD_PROJECT; |
| 23 | + |
| 24 | +/** |
| 25 | + * The samples in this file introduced how to do the auto complete, including: |
| 26 | + * |
| 27 | + * - Default auto complete (on both company display name and job title) |
| 28 | + * |
| 29 | + * - Auto complete on job title only |
| 30 | + */ |
| 31 | + |
| 32 | +//[START auto_complete_job_title] |
| 33 | + |
| 34 | +/** |
| 35 | + * Auto completes job titles within given companyName. |
| 36 | + */ |
| 37 | +const jobTitleAutoComplete = async (jobServiceClient, query, companyName) => { |
| 38 | + try { |
| 39 | + const request = { |
| 40 | + name: `projects/${PROJECT_ID}`, |
| 41 | + query: query, |
| 42 | + languageCode: 'en-US', |
| 43 | + type: 'JOB_TITLE', |
| 44 | + pageSize: 10, |
| 45 | + }; |
| 46 | + |
| 47 | + if (companyName) { |
| 48 | + request.companyName = companyName; |
| 49 | + } |
| 50 | + |
| 51 | + const complete = await jobServiceClient.projects.complete(request); |
| 52 | + |
| 53 | + console.log(JSON.stringify(complete.data)); |
| 54 | + } catch (e) { |
| 55 | + console.error(e); |
| 56 | + throw e; |
| 57 | + } |
| 58 | +}; |
| 59 | +// [END auto_complete_job_title] |
| 60 | + |
| 61 | +// [START auto_complete_default] |
| 62 | + |
| 63 | +/** |
| 64 | + * Auto completes job titles within given companyName. |
| 65 | + */ |
| 66 | +const defaultAutoComplete = async (jobServiceClient, query, companyName) => { |
| 67 | + try { |
| 68 | + const request = { |
| 69 | + name: `projects/${PROJECT_ID}`, |
| 70 | + query: query, |
| 71 | + languageCode: 'en-US', |
| 72 | + pageSize: 10, |
| 73 | + }; |
| 74 | + |
| 75 | + if (companyName) { |
| 76 | + request.companyName = companyName; |
| 77 | + } |
| 78 | + |
| 79 | + const complete = await jobServiceClient.projects.complete(request); |
| 80 | + |
| 81 | + console.log(JSON.stringify(complete.data)); |
| 82 | + } catch (e) { |
| 83 | + console.error(e); |
| 84 | + throw e; |
| 85 | + } |
| 86 | +}; |
| 87 | +// [END auto_complete_default] |
| 88 | + |
| 89 | +// Run Sample |
| 90 | +(async () => { |
| 91 | + try { |
| 92 | + // Create an authorized client |
| 93 | + const jobServiceClient = await createAuthCredential(); |
| 94 | + |
| 95 | + // Create a company |
| 96 | + const companyToBeCreated = basicCompanySample.generateCompany(); |
| 97 | + const companyCreated = await basicCompanySample.createCompany( |
| 98 | + jobServiceClient, |
| 99 | + companyToBeCreated |
| 100 | + ); |
| 101 | + const companyName = companyCreated.name; |
| 102 | + |
| 103 | + // Construct a job |
| 104 | + const jobToBeCreated = basicJobSample.generateJobWithRequiredFields( |
| 105 | + companyName |
| 106 | + ); |
| 107 | + jobToBeCreated.title = 'Software engineer'; |
| 108 | + const jobCreated = await basicJobSample.createJob( |
| 109 | + jobServiceClient, |
| 110 | + jobToBeCreated |
| 111 | + ); |
| 112 | + const jobName = jobCreated.name; |
| 113 | + |
| 114 | + await defaultAutoComplete(jobServiceClient, 'goo', companyName); |
| 115 | + await defaultAutoComplete(jobServiceClient, 'sof', companyName); |
| 116 | + await jobTitleAutoComplete(jobServiceClient, 'sof', companyName); |
| 117 | + |
| 118 | + // Delete job and company |
| 119 | + await basicJobSample.deleteJob(jobServiceClient, jobName); |
| 120 | + await basicCompanySample.deleteCompany(jobServiceClient, companyName); |
| 121 | + } catch (e) { |
| 122 | + console.error(e); |
| 123 | + throw e; |
| 124 | + } |
| 125 | +})(); |
0 commit comments