|
| 1 | +/** |
| 2 | + * Copyright 2019, 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 | +const customAttributeSample = require('./custom-attribute-sample'); |
| 22 | +const sleep = require('./sleep'); |
| 23 | + |
| 24 | +const PROJECT_ID = process.env.GOOGLE_CLOUD_PROJECT; |
| 25 | + |
| 26 | +/** |
| 27 | + * The sample in this file introduces how to perform a batch delete of jobs. |
| 28 | + * NOTE: Both company name and requisition ID are required to perform batchDelete. |
| 29 | + */ |
| 30 | + |
| 31 | +// [START jobs_batchDelete] |
| 32 | + |
| 33 | +/** |
| 34 | + * Batch delete jobs. |
| 35 | + * NOTE: @property [filter] formatting is strict. Do not change. |
| 36 | + */ |
| 37 | +const batchDelete = async (jobServiceClient, companyName, jobs) => { |
| 38 | + try { |
| 39 | + let batchDeleteQuery = `companyName = "${companyName}"`; |
| 40 | + jobs.forEach(job => { |
| 41 | + batchDeleteQuery += ` AND requisitionId = "${job.requisitionId}"`; |
| 42 | + }); |
| 43 | + |
| 44 | + const request = { |
| 45 | + parent: `projects/${PROJECT_ID}`, |
| 46 | + requestBody: { |
| 47 | + filter: batchDeleteQuery, |
| 48 | + }, |
| 49 | + }; |
| 50 | + |
| 51 | + const result = await jobServiceClient.projects.jobs.batchDelete(request); |
| 52 | + console.log(JSON.stringify('Batch deleted.', result)); |
| 53 | + return; |
| 54 | + } catch (e) { |
| 55 | + console.error(e); |
| 56 | + throw e; |
| 57 | + } |
| 58 | +}; |
| 59 | +// [END jobs_batchDelete] |
| 60 | + |
| 61 | +// [START list_jobs] |
| 62 | + |
| 63 | +/** |
| 64 | + * List Jobs. |
| 65 | + * NOTE: @property [filter] formatting is strict. Do not change. |
| 66 | + */ |
| 67 | +const listJobs = async (jobServiceClient, companyName) => { |
| 68 | + try { |
| 69 | + const request = { |
| 70 | + parent: `projects/${PROJECT_ID}`, |
| 71 | + filter: `companyName = "${companyName}"`, |
| 72 | + }; |
| 73 | + const jobsObj = await jobServiceClient.projects.jobs.list(request); |
| 74 | + |
| 75 | + console.log(JSON.stringify(jobsObj.data)); |
| 76 | + return jobsObj.data.jobs; |
| 77 | + } catch (e) { |
| 78 | + console.error(e); |
| 79 | + throw e; |
| 80 | + } |
| 81 | +}; |
| 82 | +// [END list_jobs] |
| 83 | + |
| 84 | +// Run Sample |
| 85 | +(async () => { |
| 86 | + try { |
| 87 | + // Create an authorized client |
| 88 | + const jobServiceClient = await createAuthCredential(); |
| 89 | + |
| 90 | + // Create a company |
| 91 | + const companyToBeCreated = basicCompanySample.generateCompany(); |
| 92 | + const companyCreated = await basicCompanySample.createCompany( |
| 93 | + jobServiceClient, |
| 94 | + companyToBeCreated |
| 95 | + ); |
| 96 | + const companyName = companyCreated.name; |
| 97 | + |
| 98 | + // Create multiple jobs |
| 99 | + for (let i = 0; i < 5; i += 1) { |
| 100 | + const jobToBeCreated = customAttributeSample.generateJobWithACustomAttribute( |
| 101 | + companyName |
| 102 | + ); |
| 103 | + await basicJobSample.createJob(jobServiceClient, jobToBeCreated); |
| 104 | + } |
| 105 | + |
| 106 | + // Wait several seconds for post processing |
| 107 | + await sleep(10000); |
| 108 | + |
| 109 | + // Get a list of jobs |
| 110 | + const jobs = await listJobs(jobServiceClient, companyName); |
| 111 | + |
| 112 | + // Batch delete jobs |
| 113 | + await batchDelete(jobServiceClient, companyName, jobs); |
| 114 | + } catch (e) { |
| 115 | + console.log(e); |
| 116 | + throw e; |
| 117 | + } |
| 118 | +})(); |
0 commit comments