0% found this document useful (0 votes)
5 views

firebaseImplement

The document outlines the creation of two Firestore collections, Employees and Departments, with specific keys for each document. It includes functions to add documents, display department details in real-time, retrieve the top five employees by salary, and list employees in the department with the most employees. Additionally, it calculates the total number of leaves taken by all employees combined.

Uploaded by

testrahu6
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

firebaseImplement

The document outlines the creation of two Firestore collections, Employees and Departments, with specific keys for each document. It includes functions to add documents, display department details in real-time, retrieve the top five employees by salary, and list employees in the department with the most employees. Additionally, it calculates the total number of leaves taken by all employees combined.

Uploaded by

testrahu6
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Q1) Create two collections in a database of Firestore, named Employees and Departments.

Add ten documents to Employees and five documents to Departments using the web

app. Following are the keys of documents for both of the collections.

a. Employees - name, dob, department_id, salary, age, leaves_left

b. Departments - department_name, no_employees, head_of_dept,

total_leaves_for_emp

const authorCollection = collection(firestore, 'authors');

const employeeCollection = collection(firestore, 'employees');


async function addNewEmployee (name,dob,department_id,salary,age,leaves_left){
const newEmployeeDoc = await addDoc(employeeCollection, {
name: name,
dob: dob,
department_id: department_id,
salary: salary,
age: age,
leaves_left: leaves_left})
}

await addNewEmployee("Mahesh Singh",new Date(2001,11,2),1,250000,22,15)


await addNewEmployee("Priya Sharma", new Date(1998, 5, 10), 2, 300000, 25,
10);
await addNewEmployee("Alice Young", new Date(2023, 0, 1), 4, 180000, 0, 20);
await addNewEmployee("David Lee", new Date(2000, 7, 15),5 , 220000, 3, 18);
await addNewEmployee("John Doe",new Date(2000, 7, 15),5 , 220000, 3, 18);
const departmentCollection = collection(firestore, 'departments');

async function addNewDepartment(department_name, no_employees, head_of_dept,


total_leaves_for_emp) {
const newDepartmentDoc = await addDoc(departmentCollection, {
department_name: department_name,
no_employees: no_employees,
head_of_dept: head_of_dept,
total_leaves_for_emp: total_leaves_for_emp
});
}

await addNewDepartment("Human Resources", 15, "Emma Johnson", 200);


await addNewDepartment("Marketing", 12, "Alex Smith", 180);
await addNewDepartment("Finance", 10, "Michael Brown", 220);
await addNewDepartment("Operations", 20, "Sophia Martinez", 190);
Q2) Display the details of departments in real time using the web.

const departmentCollection = collection(firestore, 'departments');

function displayDepartmentDetails() {
const unsubscribe = onSnapshot(departmentCollection, (querySnapshot) => {
querySnapshot.forEach((doc) => {
const department = doc.data();
console.log(`Department Name: ${department.department_name}`);
console.log(`Number of Employees: ${department.no_employees}`);
console.log(`Head of Department: ${department.head_of_dept}`);
console.log(`Total Leaves for Employees:
${department.total_leaves_for_emp}`);
console.log('---------------------');
});
});

// unsubscribe();
}
Q3) Display name, age, department_name, and salary of top five employees based on

salary.

const employeeCollection = collection(firestore, 'employees');

async function getTopEmployeesBySalary() {


try {
const queryEmp = query(
employeeCollection,
orderBy('salary', 'desc'),
limit(5)
);

return new Promise((resolve, reject) => {


const unsubscribe = onSnapshot(queryEmp, (querySnapshot) => {
const topEmployees = [];
querySnapshot.forEach((doc) => {
const employee = doc.data();
topEmployees.push({
name: employee.name,
salary: employee.salary,
department_id: employee.department_id,
age: employee.age,
leaves_left: employee.leaves_left
});
});
resolve(topEmployees);
}, reject);
});
} catch (error) {
console.error('Error getting top employees:', error);
throw error;
}
}
getTopEmployeesBySalary()
.then((topEmployees) => {
console.log('Top 5 Employees by Salary:');
topEmployees.forEach((employee, index) => {
console.log(`#${index + 1}`);
console.log(`Name: ${employee.name}`);
console.log(`Salary: ${employee.salary}`);
console.log(`Department ID: ${employee.department_id}`);
console.log(`Age: ${employee.age}`);
console.log(`Leaves Left: ${employee.leaves_left}`);
console.log('---------------------');
});
})
.catch((error) => {
console.error('Error getting top employees:', error);
});
Q4) Display the list of employees of the department with the most number of employees.

async function getDepartmentWithMostEmployees() {


try {
const departmentQuerySnapshot = await getDocs(query(departmentCollection,
orderBy('no_employees', 'desc'), limit(3)));

let departmentIdWithMostEmployees = null;


console.log(departmentQuerySnapshot)
departmentQuerySnapshot.forEach((doc) => {
departmentIdWithMostEmployees = doc.data().department_id;
});

return departmentIdWithMostEmployees;
} catch (error) {
console.error('Error getting department with most employees:', error);
throw error;
}
}

async function getEmployeesInDepartment(departmentId) {


try {
const employeeQuerySnapshot = await getDocs(query(employeeCollection,
where('department_id', '==', departmentId)));

const employeesInDepartment = [];


employeeQuerySnapshot.forEach((doc) => {
const employee = doc.data();
employeesInDepartment.push({
name: employee.name,
salary: employee.salary,
age: employee.age,
leaves_left: employee.leaves_left
});
});

return employeesInDepartment;
} catch (error) {
console.error('Error getting employees in department:', error);
throw error;
}
}

async function displayEmployeesInDepartmentWithMostEmployees() {


try {
const departmentIdWithMostEmployees = await
getDepartmentWithMostEmployees();
console.log(departmentIdWithMostEmployees);
if (departmentIdWithMostEmployees !== null) {
const employeesInDepartment = await
getEmployeesInDepartment(departmentIdWithMostEmployees);

console.log(`Employees in the department with the most employees


(Department ID: ${departmentIdWithMostEmployees}):`);
employeesInDepartment.forEach((employee, index) => {
console.log(`#${index + 1}`);
console.log(`Name: ${employee.name}`);
console.log(`Salary: ${employee.salary}`);
console.log(`Age: ${employee.age}`);
console.log(`Leaves Left: ${employee.leaves_left}`);
console.log('---------------------');
});
} else {
console.log('No department found.');
}
} catch (error) {
console.error('Error displaying employees in department with most
employees:', error);
}
}

displayEmployeesInDepartmentWithMostEmployees();
Q5) Determine the total number of leaves taken by each employee.

async function getTotalLeavesTaken() {


try {
const employeeQuerySnapshot = await getDocs(employeeCollection);

let totalLeavesTaken = 0;
employeeQuerySnapshot.forEach((doc) => {
const employee = doc.data();
totalLeavesTaken += employee.leaves_left;
});

return totalLeavesTaken;
} catch (error) {
console.error('Error getting total leaves taken:', error);
throw error;
}
}

getTotalLeavesTaken()
.then((totalLeavesTaken) => {
console.log(`Total number of leaves taken by all employees combined:
${totalLeavesTaken}`);
})
.catch((error) => {
console.error('Error getting total leaves taken:', error);
});

You might also like