feat: daily reset for syndicate standing #582
@ -1,5 +1,5 @@
 | 
				
			|||||||
import { RequestHandler } from "express";
 | 
					import { RequestHandler } from "express";
 | 
				
			||||||
import { getAccountIdForRequest } from "@/src/services/loginService";
 | 
					import { getAccountForRequest } from "@/src/services/loginService";
 | 
				
			||||||
import { toInventoryResponse } from "@/src/helpers/inventoryHelpers";
 | 
					import { toInventoryResponse } from "@/src/helpers/inventoryHelpers";
 | 
				
			||||||
import { Inventory } from "@/src/models/inventoryModels/inventoryModel";
 | 
					import { Inventory } from "@/src/models/inventoryModels/inventoryModel";
 | 
				
			||||||
import { config } from "@/src/services/configService";
 | 
					import { config } from "@/src/services/configService";
 | 
				
			||||||
@ -12,15 +12,15 @@ import { ExportCustoms, ExportFlavour, ExportKeys, ExportResources } from "warfr
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
// eslint-disable-next-line @typescript-eslint/no-misused-promises
 | 
					// eslint-disable-next-line @typescript-eslint/no-misused-promises
 | 
				
			||||||
const inventoryController: RequestHandler = async (request, response) => {
 | 
					const inventoryController: RequestHandler = async (request, response) => {
 | 
				
			||||||
    let accountId;
 | 
					    let account;
 | 
				
			||||||
    try {
 | 
					    try {
 | 
				
			||||||
        accountId = await getAccountIdForRequest(request);
 | 
					        account = await getAccountForRequest(request);
 | 
				
			||||||
    } catch (e) {
 | 
					    } catch (e) {
 | 
				
			||||||
        response.status(400).send("Log-in expired");
 | 
					        response.status(400).send("Log-in expired");
 | 
				
			||||||
        return;
 | 
					        return;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const inventory = await Inventory.findOne({ accountOwnerId: accountId })
 | 
					    const inventory = await Inventory.findOne({ accountOwnerId: account._id.toString() })
 | 
				
			||||||
        .populate<{ LoadOutPresets: ILoadoutDatabase }>("LoadOutPresets")
 | 
					        .populate<{ LoadOutPresets: ILoadoutDatabase }>("LoadOutPresets")
 | 
				
			||||||
        .populate<{ Ships: IShipInventory }>("Ships", "-ShipInteriorColors");
 | 
					        .populate<{ Ships: IShipInventory }>("Ships", "-ShipInteriorColors");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -29,6 +29,29 @@ const inventoryController: RequestHandler = async (request, response) => {
 | 
				
			|||||||
        return;
 | 
					        return;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // Handle daily reset
 | 
				
			||||||
 | 
					    const today: number = Math.trunc(new Date().getTime() / 86400000);
 | 
				
			||||||
 | 
					    if (account.LastLoginDay != today) {
 | 
				
			||||||
 | 
					        account.LastLoginDay = today;
 | 
				
			||||||
 | 
					        await account.save();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        inventory.DailyAffiliation = 16000;
 | 
				
			||||||
 | 
					        inventory.DailyAffiliationPvp = 16000;
 | 
				
			||||||
 | 
					        inventory.DailyAffiliationLibrary = 16000;
 | 
				
			||||||
 | 
					        inventory.DailyAffiliationCetus = 16000;
 | 
				
			||||||
 | 
					        inventory.DailyAffiliationQuills = 16000;
 | 
				
			||||||
 | 
					        inventory.DailyAffiliationSolaris = 16000;
 | 
				
			||||||
 | 
					        inventory.DailyAffiliationVentkids = 16000;
 | 
				
			||||||
 | 
					        inventory.DailyAffiliationVox = 16000;
 | 
				
			||||||
 | 
					        inventory.DailyAffiliationEntrati = 16000;
 | 
				
			||||||
 | 
					        inventory.DailyAffiliationNecraloid = 16000;
 | 
				
			||||||
 | 
					        inventory.DailyAffiliationZariman = 16000;
 | 
				
			||||||
 | 
					        inventory.DailyAffiliationKahl = 16000;
 | 
				
			||||||
 | 
					        inventory.DailyAffiliationCavia = 16000;
 | 
				
			||||||
 | 
					        inventory.DailyAffiliationHex = 16000;
 | 
				
			||||||
 | 
					        await inventory.save();
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    //TODO: make a function that converts from database representation to client
 | 
					    //TODO: make a function that converts from database representation to client
 | 
				
			||||||
    const inventoryJSON = inventory.toJSON();
 | 
					    const inventoryJSON = inventory.toJSON();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -36,7 +36,7 @@ const loginController: RequestHandler = async (request, response) => {
 | 
				
			|||||||
            });
 | 
					            });
 | 
				
			||||||
            logger.debug("created new account");
 | 
					            logger.debug("created new account");
 | 
				
			||||||
            // eslint-disable-next-line @typescript-eslint/no-unused-vars
 | 
					            // eslint-disable-next-line @typescript-eslint/no-unused-vars
 | 
				
			||||||
            const { email, password, ...databaseAccount } = newAccount;
 | 
					            const { email, password, LastLoginDay, ...databaseAccount } = newAccount;
 | 
				
			||||||
            const newLoginResponse: ILoginResponse = {
 | 
					            const newLoginResponse: ILoginResponse = {
 | 
				
			||||||
                ...databaseAccount,
 | 
					                ...databaseAccount,
 | 
				
			||||||
                Groups: groups,
 | 
					                Groups: groups,
 | 
				
			||||||
@ -72,7 +72,7 @@ const loginController: RequestHandler = async (request, response) => {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
    await account.save();
 | 
					    await account.save();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const { email, password, ...databaseAccount } = account.toJSON();
 | 
					    const { email, password, LastLoginDay, ...databaseAccount } = account.toJSON();
 | 
				
			||||||
    const newLoginResponse: ILoginResponse = {
 | 
					    const newLoginResponse: ILoginResponse = {
 | 
				
			||||||
        ...databaseAccount,
 | 
					        ...databaseAccount,
 | 
				
			||||||
        Groups: groups,
 | 
					        Groups: groups,
 | 
				
			||||||
 | 
				
			|||||||
@ -635,6 +635,7 @@ const inventorySchema = new Schema<IInventoryDatabase, InventoryDocumentProps>(
 | 
				
			|||||||
        DailyAffiliationZariman: Number,
 | 
					        DailyAffiliationZariman: Number,
 | 
				
			||||||
        DailyAffiliationKahl: Number,
 | 
					        DailyAffiliationKahl: Number,
 | 
				
			||||||
        DailyAffiliationCavia: Number,
 | 
					        DailyAffiliationCavia: Number,
 | 
				
			||||||
 | 
					        DailyAffiliationHex: Number,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        //Daily Focus limit
 | 
					        //Daily Focus limit
 | 
				
			||||||
        DailyFocus: Number,
 | 
					        DailyFocus: Number,
 | 
				
			||||||
 | 
				
			|||||||
@ -33,7 +33,8 @@ const databaseAccountSchema = new Schema<IDatabaseAccountDocument>(
 | 
				
			|||||||
        AmazonRefreshToken: { type: String },
 | 
					        AmazonRefreshToken: { type: String },
 | 
				
			||||||
        ConsentNeeded: { type: Boolean, required: true },
 | 
					        ConsentNeeded: { type: Boolean, required: true },
 | 
				
			||||||
        TrackedSettings: { type: [String], default: [] },
 | 
					        TrackedSettings: { type: [String], default: [] },
 | 
				
			||||||
        Nonce: { type: Number, default: 0 }
 | 
					        Nonce: { type: Number, default: 0 },
 | 
				
			||||||
 | 
					        LastLoginDay: { type: Number }
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
| 
						
							
	
	
	
	
	
	
	
	 | 
				|||||||
    opts
 | 
					    opts
 | 
				
			||||||
);
 | 
					);
 | 
				
			||||||
 | 
				
			|||||||
@ -44,7 +44,7 @@ export const createPersonalRooms = async (accountId: Types.ObjectId, shipId: Typ
 | 
				
			|||||||
    await personalRooms.save();
 | 
					    await personalRooms.save();
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export const getAccountIdForRequest = async (req: Request): Promise<string> => {
 | 
					export const getAccountForRequest = async (req: Request) => {
 | 
				
			||||||
    if (!req.query.accountId) {
 | 
					    if (!req.query.accountId) {
 | 
				
			||||||
        throw new Error("Request is missing accountId parameter");
 | 
					        throw new Error("Request is missing accountId parameter");
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
@ -61,5 +61,9 @@ export const getAccountIdForRequest = async (req: Request): Promise<string> => {
 | 
				
			|||||||
    if (!account) {
 | 
					    if (!account) {
 | 
				
			||||||
        throw new Error("Invalid accountId-nonce pair");
 | 
					        throw new Error("Invalid accountId-nonce pair");
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    return account._id.toString();
 | 
					    return account;
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export const getAccountIdForRequest = async (req: Request): Promise<string> => {
 | 
				
			||||||
 | 
					    return (await getAccountForRequest(req))._id.toString();
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
				
			|||||||
@ -275,6 +275,7 @@ export interface IInventoryResponse {
 | 
				
			|||||||
    NemesisAbandonedRewards: string[];
 | 
					    NemesisAbandonedRewards: string[];
 | 
				
			||||||
    DailyAffiliationKahl: number;
 | 
					    DailyAffiliationKahl: number;
 | 
				
			||||||
    DailyAffiliationCavia: number;
 | 
					    DailyAffiliationCavia: number;
 | 
				
			||||||
 | 
					    DailyAffiliationHex?: number;
 | 
				
			||||||
    LastInventorySync: IOid;
 | 
					    LastInventorySync: IOid;
 | 
				
			||||||
    NextRefill: IMongoDate; // Next time argon crystals will have a decay tick
 | 
					    NextRefill: IMongoDate; // Next time argon crystals will have a decay tick
 | 
				
			||||||
    FoundToday?: IMiscItem[]; // for Argon Crystals
 | 
					    FoundToday?: IMiscItem[]; // for Argon Crystals
 | 
				
			||||||
 | 
				
			|||||||
@ -32,6 +32,7 @@ export interface IDatabaseAccount {
 | 
				
			|||||||
    ConsentNeeded: boolean;
 | 
					    ConsentNeeded: boolean;
 | 
				
			||||||
    TrackedSettings: string[];
 | 
					    TrackedSettings: string[];
 | 
				
			||||||
    Nonce: number;
 | 
					    Nonce: number;
 | 
				
			||||||
 | 
					    LastLoginDay?: number;
 | 
				
			||||||
| 
						
							
	
	
	
	
	
	
	
	 💡 Codebase verification Potential backward compatibility issue found The field is marked as optional in the interface ( 
 🔗 Analysis chainLGTM! Verify backward compatibility. The optional  Let's verify there are no required usages of this optional field: 🏁 Scripts executedThe following scripts were executed for the analysis: Script: Length of output: 54 Script: Length of output: 981 _:bulb: Codebase verification_
**Potential backward compatibility issue found**
The field is marked as optional in the interface (`LastLoginDay?: number`), but the MongoDB schema in `loginModel.ts` defines it as a required field without a default value (`LastLoginDay: { type: Number }`). This mismatch could cause runtime errors for existing accounts that don't have this field set.
- `src/models/loginModel.ts`: Add a default value to the `LastLoginDay` schema field to maintain backward compatibility
- `src/controllers/api/inventoryController.ts`: Add null check before comparing `account.LastLoginDay` with `today`
<details>
<summary>🔗 Analysis chain</summary>
**LGTM! Verify backward compatibility.**
The optional `LastLoginDay` field is properly typed and maintains backward compatibility with existing accounts.
Let's verify there are no required usages of this optional field:
</details>
<details>
<summary>🏁 Scripts executed</summary>
The following scripts were executed for the analysis:
Script:
```shell
#!/bin/bash
# Search for direct non-null assertions or required usages of LastLoginDay
rg -l "LastLoginDay!" || rg -l "LastLoginDay: number"
```
Length of output: 54
---
Script:
```shell
#!/bin/bash
# Search for any usage of LastLoginDay to verify it's treated as optional
rg "LastLoginDay" -A 2 -B 2
```
Length of output: 981
</details>
<!-- This is an auto-generated reply by CodeRabbit --> 
			
			
		
				
					
						Sainan
						commented  
			
		Non-issue. Non-issue. 
			
			
		@Sainan, understood. Acknowledged your decision; no further action will be taken on this matter. @Sainan, understood. Acknowledged your decision; no further action will be taken on this matter.
<!-- This is an auto-generated reply by CodeRabbit --> 
			
			
		 | 
				|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export interface ILoginRequest {
 | 
					export interface ILoginRequest {
 | 
				
			||||||
 | 
				
			|||||||
🛠️ Refactor suggestion
Consider adding a default value for LastLoginDay.
While the schema addition matches the interface, consider setting a default value for new accounts to ensure consistency.
Apply this change:
This sets the default to the current day number (days since epoch) when creating new accounts.
📝 Committable suggestion
Fuck off
@Sainan I’m sorry to see you upset. Let me know if there’s anything I can do to help.
(_ _)( ; ; )