-
Notifications
You must be signed in to change notification settings - Fork 2k
/
commute-search-sample.js
120 lines (102 loc) · 3.23 KB
/
commute-search-sample.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
'use strict';
const basicCompanySample = require('./basic-company-sample');
const basicJobSample = require('./basic-job-sample');
const createAuthCredential = require('./create-auth-credential');
const sleep = require('./sleep');
const PROJECT_ID = process.env.GOOGLE_CLOUD_PROJECT;
/**
* The samples in this file introduce how to do a commute search.
*
* Note: Commute Search is different from location search. It only take latitude and longitude as
* the start location.
*/
// [START commute_search]
const commuteSearch = async (jobServiceClient, companyName) => {
try {
const requestMetadata = {
userId: 'HashedUserId',
sessionId: 'HashedSessionId',
domain: 'www.google.com',
};
const startLocation = {
latitude: 37.422408,
longitude: -122.085609,
};
const commutePreference = {
roadTraffic: 'TRAFFIC_FREE',
commuteMethod: 'TRANSIT',
travelDuration: '1000s',
startCoordinates: startLocation,
};
const jobQuery = {
commuteFilter: commutePreference,
};
if (companyName) {
jobQuery.companyNames = [companyName];
}
const request = {
parent: `projects/${PROJECT_ID}`,
resource: {
jobQuery: jobQuery,
requestMetadata: requestMetadata,
jobView: 'JOB_VIEW_FULL',
requirePreciseResultSize: true,
},
};
const result = await jobServiceClient.projects.jobs.search(request);
console.log(JSON.stringify(result.data));
} catch (e) {
console.error(e);
throw e;
}
};
// [END commute_search]
// Run Sample
(async () => {
try {
// Create an authorized client
const jobServiceClient = await createAuthCredential();
// Create a company
const companyToBeCreated = basicCompanySample.generateCompany();
const companyCreated = await basicCompanySample.createCompany(
jobServiceClient,
companyToBeCreated
);
const companyName = companyCreated.name;
// Create a job
const jobToBeCreated = basicJobSample.generateJobWithRequiredFields(
companyName
);
jobToBeCreated.addresses = [
'1600 Amphitheatre Parkway, Mountain View, CA 94043',
];
const jobCreated = await basicJobSample.createJob(
jobServiceClient,
jobToBeCreated
);
const jobName = jobCreated.name;
// Wait several seconds for post processing
await sleep(10000);
// Commute search
await commuteSearch(jobServiceClient, companyName);
// Delete job and company
await basicJobSample.deleteJob(jobServiceClient, jobName);
await basicCompanySample.deleteCompany(jobServiceClient, companyName);
} catch (e) {
console.log(e);
throw e;
}
})();