Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

feat: print companies to delete in weekly job

Brad Barbin8cf903ecc70a
diff --git a/packages/database/src/types.ts b/packages/database/src/types.ts
index 143d9a8672..e77102e436 100644
--- a/packages/database/src/types.ts
+++ b/packages/database/src/types.ts
@@ -1977,6 +1977,7 @@ export type Database = {
baseCurrencyCode: string
city: string | null
countryCode: string | null
+ createdAt: string
email: string | null
externalId: Json | null
fax: string | null
@@ -2001,6 +2002,7 @@ export type Database = {
baseCurrencyCode: string
city?: string | null
countryCode?: string | null
+ createdAt?: string
email?: string | null
externalId?: Json | null
fax?: string | null
@@ -2025,6 +2027,7 @@ export type Database = {
baseCurrencyCode?: string
city?: string | null
countryCode?: string | null
+ createdAt?: string
email?: string | null
externalId?: Json | null
fax?: string | null
@@ -43280,14 +43283,14 @@ export type Database = {
Relationships: [
{
foreignKeyName: "address_countryCode_fkey"
- columns: ["supplierCountryCode"]
+ columns: ["customerCountryCode"]
isOneToOne: false
referencedRelation: "country"
referencedColumns: ["alpha2"]
},
{
foreignKeyName: "address_countryCode_fkey"
- columns: ["customerCountryCode"]
+ columns: ["supplierCountryCode"]
isOneToOne: false
referencedRelation: "country"
referencedColumns: ["alpha2"]
@@ -46342,14 +46345,14 @@ export type Database = {
Relationships: [
{
foreignKeyName: "address_countryCode_fkey"
- columns: ["paymentCountryCode"]
+ columns: ["customerCountryCode"]
isOneToOne: false
referencedRelation: "country"
referencedColumns: ["alpha2"]
},
{
foreignKeyName: "address_countryCode_fkey"
- columns: ["customerCountryCode"]
+ columns: ["paymentCountryCode"]
isOneToOne: false
referencedRelation: "country"
referencedColumns: ["alpha2"]
diff --git a/packages/database/supabase/functions/lib/types.ts b/packages/database/supabase/functions/lib/types.ts
index 143d9a8672..e77102e436 100644
--- a/packages/database/supabase/functions/lib/types.ts
+++ b/packages/database/supabase/functions/lib/types.ts
@@ -1977,6 +1977,7 @@ export type Database = {
baseCurrencyCode: string
city: string | null
countryCode: string | null
+ createdAt: string
email: string | null
externalId: Json | null
fax: string | null
@@ -2001,6 +2002,7 @@ export type Database = {
baseCurrencyCode: string
city?: string | null
countryCode?: string | null
+ createdAt?: string
email?: string | null
externalId?: Json | null
fax?: string | null
@@ -2025,6 +2027,7 @@ export type Database = {
baseCurrencyCode?: string
city?: string | null
countryCode?: string | null
+ createdAt?: string
email?: string | null
externalId?: Json | null
fax?: string | null
@@ -43280,14 +43283,14 @@ export type Database = {
Relationships: [
{
foreignKeyName: "address_countryCode_fkey"
- columns: ["supplierCountryCode"]
+ columns: ["customerCountryCode"]
isOneToOne: false
referencedRelation: "country"
referencedColumns: ["alpha2"]
},
{
foreignKeyName: "address_countryCode_fkey"
- columns: ["customerCountryCode"]
+ columns: ["supplierCountryCode"]
isOneToOne: false
referencedRelation: "country"
referencedColumns: ["alpha2"]
@@ -46342,14 +46345,14 @@ export type Database = {
Relationships: [
{
foreignKeyName: "address_countryCode_fkey"
- columns: ["paymentCountryCode"]
+ columns: ["customerCountryCode"]
isOneToOne: false
referencedRelation: "country"
referencedColumns: ["alpha2"]
},
{
foreignKeyName: "address_countryCode_fkey"
- columns: ["customerCountryCode"]
+ columns: ["paymentCountryCode"]
isOneToOne: false
referencedRelation: "country"
referencedColumns: ["alpha2"]
diff --git a/packages/database/supabase/migrations/20251024175413_company-created-at.sql b/packages/database/supabase/migrations/20251024175413_company-created-at.sql
new file mode 100644
index 0000000000..f846ffcc39
--- /dev/null
+++ b/packages/database/supabase/migrations/20251024175413_company-created-at.sql
@@ -0,0 +1 @@
+ALTER TABLE "company" ADD COLUMN "createdAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now();
\ No newline at end of file
diff --git a/packages/jobs/trigger/weekly.ts b/packages/jobs/trigger/weekly.ts
index 3a39358a45..82ebed4b82 100644
--- a/packages/jobs/trigger/weekly.ts
+++ b/packages/jobs/trigger/weekly.ts
@@ -27,6 +27,68 @@ export const weekly = schedules.task({
const bypassList = bypassData.bypassList ?? [];
console.log(`Bypass list: ${bypassList}`);
+
+ // Get all companies
+ const { data: companies, error: companiesError } = await serviceRole
+ .from("company")
+ .select("id, name, createdAt");
+
+ if (companiesError) {
+ console.error(`Failed to fetch companies: ${companiesError.message}`);
+ return;
+ }
+
+ console.log(`Found ${companies?.length || 0} companies`);
+
+ // Get all company plans
+ const { data: companyPlans, error: plansError } = await serviceRole
+ .from("companyPlan")
+ .select("id, stripeSubscriptionStatus");
+
+ if (plansError) {
+ console.error(`Failed to fetch company plans: ${plansError.message}`);
+ return;
+ }
+
+ // Create a map of company plans for quick lookup
+ const planMap = new Map(
+ companyPlans?.map((plan) => [
+ plan.id,
+ plan.stripeSubscriptionStatus,
+ ]) || []
+ );
+
+ // Filter companies to delete
+ const oneWeekAgo = new Date();
+ oneWeekAgo.setDate(oneWeekAgo.getDate() - 7);
+
+ const companiesToDelete =
+ companies?.filter((company) => {
+ // Keep companies with a plan that is not canceled
+ if (planMap.get(company.id) !== "Canceled") {
+ return false;
+ }
+
+ // Keep companies in bypass list
+ if (bypassList.includes(company.id)) {
+ return false;
+ }
+
+ // // Keep companies created in the last week
+ // const createdAt = new Date(company.createdAt);
+ // if (createdAt > oneWeekAgo) {
+ // return false;
+ // }
+
+ // Delete this company
+ return true;
+ }) || [];
+
+ console.log(`Companies to delete: ${companiesToDelete.length}`);
+
+ for (const company of companiesToDelete) {
+ console.log(`Deleting company ${company.name}`);
+ }
}
console.log(`📅 Weekly tasks completed: ${new Date().toISOString()}`);