I have a document of transactions where a transaction can be of different shops.
I want to query transactions with certain id in certain date. I have a field for date and id which is a MongoDB id, when I query using
Transaction.find({ $and: [{ date: nepali_date.day, shop_id: user.shop_id }] }).lean()
It returns data of all the transaction with the date given, I don't want that, I want the data of date 14 AND shop id "61f3c70c89e5453d271e0ea7"
but instead it returns:
{
"_id": "61f3c81a1410f50644d981b6",
"year": 2078,
"month": 9,
"date": 14,
"title": "Krus",
"amount": 60,
"type": "income",
"author": "Eclipsu",
"authorID": "61ef7e9565a4a88874937e96",
"shopID": "61f3c70c89e5453d271e0ea7",
"edited": false
},
{
"_id": "61f3c89ce0ea4276ddba693f",
"year": 2078,
"month": 9,
"date": 14,
"title": "RAJ",
"amount": 50,
"type": "income",
"author": "Eclipsu",
"authorID": "61ef7e9565a4a88874937e96",
"shopID": "61ef7ea165a4a88874937e9d",
"edited": false
}```
Related
I am learning Strapi in version 4. I have following two content-types:
employee with name and dept of an employee with relation salaries
salary with month, year, salaryAmount and relation employee
One employee can have many salaries for different months and years. There are three employees with
salaries of January to March 2022. So, salary content-type has nine records. Both the content-types are public.
What I am trying to attain is create a custom controller which can give data of all salaries of a
particular employee. For example if I pass employee id in browser query like
http://localhost:1337/api/empsalary/2 the response should give me three salaries of an employee
with id 2 for three months January to March 2022.
My code for controller salary.js reads as under:
'use strict';
const { createCoreController } = require('#strapi/strapi').factories;
module.exports = createCoreController('api::salary.salary', ({strapi})=>({
async empsalary (ctx){
const { id } = ctx.params;
const entity = await strapi.service('api::salary.salary').find( { employee : id } ) // pass employee id as parameter to find()
const sEntity = await this.sanitizeOutput(entity, ctx)
return this.transformResponse(sEntity)
},
}) );
The custom path file named empsalary.js reads as under:
module.exports ={
routes: [
{
method: 'GET',
path: '/empsalary/:id',
handler: 'salary.empsalary',
config: { auth : false, }
},
]
}
The result I get is as under:
{
"data": {
"attributes": {
"results": [
{
"id": 1,
"month": "January",
"year": 2022,
"salaryAmount": 65000,
"createdAt": "2022-09-21T10:36:18.314Z",
"updatedAt": "2022-09-21T10:36:19.870Z",
"publishedAt": "2022-09-21T10:36:19.866Z"
},
{
"id": 2,
"month": "January",
"year": 2022,
"salaryAmount": 50000,
"createdAt": "2022-09-21T10:36:51.059Z",
"updatedAt": "2022-09-21T10:36:56.643Z",
"publishedAt": "2022-09-21T10:36:56.640Z"
},
{
"id": 3,
"month": "January",
"year": 2022,
"salaryAmount": 40000,
"createdAt": "2022-09-21T10:37:33.949Z",
"updatedAt": "2022-09-21T10:37:35.290Z",
"publishedAt": "2022-09-21T10:37:35.288Z"
},
{
"id": 4,
"month": "February",
"year": 2022,
"salaryAmount": 70000,
"createdAt": "2022-09-21T10:39:19.440Z",
"updatedAt": "2022-09-21T10:39:21.160Z",
"publishedAt": "2022-09-21T10:39:21.158Z"
},
{
"id": 5,
"month": "February",
"year": 2022,
"salaryAmount": 60000,
"createdAt": "2022-09-21T10:39:52.514Z",
"updatedAt": "2022-09-21T10:39:54.246Z",
"publishedAt": "2022-09-21T10:39:54.244Z"
},
{
"id": 6,
"month": "February",
"year": 2022,
"salaryAmount": 40000,
"createdAt": "2022-09-21T10:40:29.293Z",
"updatedAt": "2022-09-21T10:41:10.671Z",
"publishedAt": "2022-09-21T10:40:30.679Z"
},
{
"id": 7,
"month": "March",
"year": 2022,
"salaryAmount": 75000,
"createdAt": "2022-09-21T10:43:01.475Z",
"updatedAt": "2022-09-21T10:43:03.085Z",
"publishedAt": "2022-09-21T10:43:03.082Z"
},
{
"id": 8,
"month": "March",
"year": 2022,
"salaryAmount": 60000,
"createdAt": "2022-09-21T10:43:41.065Z",
"updatedAt": "2022-09-21T10:43:42.480Z",
"publishedAt": "2022-09-21T10:43:42.478Z"
},
{
"id": 9,
"month": "March",
"year": 2022,
"salaryAmount": 50000,
"createdAt": "2022-09-21T10:44:12.663Z",
"updatedAt": "2022-09-21T10:44:14.162Z",
"publishedAt": "2022-09-21T10:44:14.160Z"
}
],
"pagination": {
"page": 1,
"pageSize": 25,
"pageCount": 1,
"total": 9
}
}
},
"meta": {}
}
The response gives me data of all nine salaries instead of only three salaries
of a particular employee.
Is there anything fundamentally wrong in my approach or even my code?
Please guide me. Thanks.
Currently using MongoDB Atlas for a Timesheet system. I have 4 collections in it:
category (has the record of clock in types and staff roles)
employees (has the record of the staff name and a unique pin)
timesheets (has the record of a Pin with the clock in type and date and time)
users (Admin login details)
Data of employees is as follows:
{
"_id": {
"$oid": "62cf98457"
},
"name": "Name Test",
"pin": "1234",
"role": "Sales",
"site": "Port Melbourne",
"email": "test#gmail.com",
"phone": "999 999 999",
"dob": "2022-07-10",
}
Data of timesheet is as follows:
{
"_id": {
"$oid": "62cf98e47caaa2ac70fba490"
},
"pin": "1234",
"type": "in",
"date": "18-01-2022",
"time": "Tuesday, January 18, 2022 6:25 AM",
"image": "image file url"
},{
"_id": {
"$oid": "62cf98e47caaa2ac70fba490"
},
"pin": "1234",
"type": "out",
"date": "18-01-2022",
"time": "Tuesday, January 18, 2022 4:25 PM",
"image": "image file url"
}
For now I am querying the employees collection if pin exits or not and if it does filtering in the timesheets collection which has same pin values again filtering them by date and then by types with time to get all employees timesheets details is a list with date.
This process is taking long for a query.
Can we directly create a new collections example employees_timesheets which will group user details and create an object inside filtering dates from timesheets collection.
Example:
{
"_id": {
"$oid": "62"
},
"name": "Name Test",
"pin": "1234",
"role": "Sales",
"date": "18-01-2022",
{
"18-01-2022": [{
"type": "in",
"time": "Tuesday, January 18, 2022 6:25 AM",
"image": "image file url"
},{
"type": "out",
"time": "Tuesday, January 18, 2022 4:25 AM",
"image": "image file url"
}]
}
}
I want to delete an object from a nested array by comparing another field in the Mongo database by using mongoose in javascript. Here is my schema.
{
"userId": "5e80e3021c5c461433807173",
"selected": [
{
"productId": "5e80e664602315159a2959da",
"selectedQty": 1,
"isAvailable": true,
"name": "Nike Mountain Shoe",
"category": "Shoes",
"qty": 2497,
"price": 1500,
"imageUrl": "aHR0cHM6Ly93d3cucmFua2FuZHN0eWxlLmNvbS9tZWRpYS9wcm9kdWN0cy9qL2phbWVzLXBlcnNlLWxvbmctc2xlZXZlLXQtc2hpcnQtbWVucy1sb25nLXNsZWUuanBn"
},
{
"productId": "5e80e4893db96d14947dce80",
"selectedQty": 1,
"isAvailable": false,
"name": "Blimper Men Shoe",
"category": "Shoes",
"qty": 2497,
"price": 500,
"imageUrl": "aHR0cHM6Ly93d3cucmFua2FuZHN0eWxlLmNvbS9tZWRpYS9wcm9kdWN0cy9qL2phbWVzLXBlcnNlLWxvbmctc2xlZXZlLXQtc2hpcnQtbWVucy1sb25nLXNsZWUuanBn"
}
]
}
How can I delete a product by comparing userId and productId?
I have a universal variable on my website which includes line items with relevant details. These line items are reflective of what the user has in their cart. I am integrating with a third party who require the data passed through to them to be formatted slightly different. The below is the data layer currently on my website:
"lineItems": [
{
"product": {
"id": "s83y016b5",
"sku_code": "s83y016b5",
"url": "/en-gb/jeans/p/s83y016b5",
"image_url": "http://www.my-website.com/a/p/shirt.jpeg",
"name": "Jeans",
"manufacturer": "",
"category": "Everyday Wear",
"stock": 116,
"currency": "GBP",
"unit_sale_price": 16,
"unit_price": 16,
"size": "6-9 Months",
"color": "Indigo"
},
"quantity": 1
}
]
The below is what format the third party needs:
"lineItems": [
{
"sku": "s83y016b5",
"name": "Jeans",
"description": "A super great pair of jeans.",
"category": "Everyday Wear",
"other": {"fieldName": "This can be a string or any value you like"}
"unitPrice": 11.99,
"salePrice": 11.99,
"quantity": 2,
"totalPrice": 23.98
"imageUrl": "http://www.my-website.com/a/p/shirt.jpeg",
"productUrl": "http://www.my-website.com/index.php/shirt.html",
}]
Obviously this needs to be dynamic based on the products in the cart. What I intend to do is use javascript to amend the data and send this to the third party via Google Tag Manager.
Any help would be greatly appreciated. Any questions welcome.
This should be close to what you're looking for.
let oldLineItems = "your object";
let newLineItems = {};
newLineItems.lineItems = [];
for (let i in oldLineItems.lineItems) {
newLineItems.lineItems[i] = {};
for (let key in oldLineItems.lineItems[i].product)
{
newLineItems.lineItems[i][key] = oldLineItems.lineItems[i].product[key];
}
}
See code below.
I'm not sure how your lineItems object is set up, but below I just created an array called line Items. If line items is a key in an object which I suspect from your snippet above, you will have to go a level deeper in the for loops used in my example below.
Simply add further details to the new object in the nested for in loops below.
var lineItems =
[
{
"product": {
"id": "s83y016b5",
"sku_code": "s83y016b5",
"url": "/en-gb/jeans/p/s83y016b5",
"image_url": "http://www.my-website.com/a/p/shirt.jpeg",
"name": "Jeans",
"manufacturer": "",
"category": "Everyday Wear",
"stock": 116,
"currency": "GBP",
"unit_sale_price": 16,
"unit_price": 16,
"size": "6-9 Months",
"color": "Indigo",
"description": 'Some random description'
},
"quantity": 1
},
{
"product": {
"id": "s83y01699",
"sku_code": "s83y01699",
"url": "/en-gb/pants/p/s83y016b5",
"image_url": "http://www.my-website.com/a/p/pants.jpeg",
"name": "Pants",
"manufacturer": "",
"category": "Casual Wear",
"stock": 90,
"currency": "au",
"unit_sale_price": 14,
"unit_price": 14,
"size": "6-9 Months",
"color": "Indigo",
"description": 'Some random description'
},
"quantity": 14
},
];
var newLineItems = [];
for(var char in lineItems){
// Adding some values to newLineItems.
newLineItems.push({
sku: lineItems[char].product.sku_code,
name: lineItems[char].product.name,
description: lineItems[char].product.description,
category: lineItems[char].product.category,
quantity: lineItems[char].quantity
});
}
console.log(JSON.stringify(newLineItems));
i want to create a form which can submit the values as the below format
[
{
"_id": "59817f39808768495728ae94",
"updatedAt": "2017-08-02T07:33:00.578Z",
"createdAt": "2017-08-02T07:28:57.310Z",
"farmId": "tesform",
"farmName": "tesform",
"gatewayId": "123",
"gatewayName": "testform getway",
"userId": "abc#kisanraja.com",
"description": "test details 1st time",
"nodes": [
{
"permanentAddress": "",
"description": "with temparature",
"name": "Node 1",
"_id": "5981802c808768495728ae9b",
"sensors": [
{
"units": "C",
"max": 60,
"min": -6,
"name": "temparature",
"_id": "5981802c808768495728ae9c"
}
],
"shortAddress": 0
},
{
"permanentAddress": "",
"description": "with soil moisture and voltage",
"name": "Node 2",
"_id": "5981802c808768495728ae98",
"sensors": [
{
"units": "C",
"max": 60,
"min": -6,
"name": "moisture",
"_id": "5981802c808768495728ae9a"
},
{
"units": "C",
"max": 60,
"min": -6,
"name": "voltage",
"_id": "5981802c808768495728ae99"
}
],
"shortAddress": 0
}
]
}
]
I want to create the nodes and sensors dynamically using jquery. Each farm should have more than one node, and each node may have more than one sensor.
You can add different class names to different level inputs, then each them, when came cross nodes, add nodes to object, when came cross to sensors, then add sensors to array as an attributes of node object named sensors when submit the form:
$("#form").submit(function() {
//deal with them;
});