Regarding fetch new collection ids

Hello guys,

Hope you are doing well,
Suppose, I have two collection users, promocodes. I want to send specific promocode to selected users. How can I do that? I will eaither send mail or text msg but my concern is how to connect promocodes and users?

Hello @Mayur_Malaviya :wave:

I think the best way to achieve what you are looking for is to use smart actions!

Check the documentation on how to create and manage Smart Actions

Tell me if it helps :wink:

@Guillaume_Cisco I have already created many smart actions in my project, so as I understand my requirement, smart action won’t help me because it will help if I would like to perform action on specific raw of that collection only but my requirement is different.
After selecting promocode, I would like to send that promocode to specific user, so whole list of users will be shown and I will choose either all or some users then send the promocode to them.
Please, suggest me other way or in smart action if I left anything.

Thanks

Smart Actions is the key here.
Either you put a smart action on your users collection and you select users to send promocodes.
Or, you put a smart action on your promocodes collection which I do not recommend as listing users in your smart action form will be very resource consuming.

Are you familiar with smart action forms?

@Guillaume_Cisco Nice suggestions. Yes, I am familiar with smart action forms.
As you suggest let us keep smart action forms on my users collection but now issue is that suppose I have 5 promocodes in that collection and some of them are expired. Now, I would like to send specific promocodes. How can I do that?

You will need to filter the promocodes by the non expired ones.
Have a look at the load hook smart action, I think it can help :wink:

@Guillaume_Cisco I think still you can’t understand my issue. let me describe it.
If I keep smart action in users collections then I need to choose promocode, which promocode I wants to send, let suppose it provide promocode 1,2,3 then I choose 2. It must send promocode 2 to all users.
As you mention above link, it helps if I would like to take any number or text input from user then it would be helpful but here, we have promocode collections and I wants to choose from them.

@Mayur_Malaviya,

I perfectly understand your problem.
You need to create a smart action with a smart action form and define a field which will be an enum for your promo codes.
This way, you will be able to select some users, then select a promo code, and on your server you will be able to send to all your selected users, an email with a promo code.
Don’t forget to use a field with an Enum type + use the load hook to load the expected promo codes.

Cheers,

@Guillaume_Cisco Thanks for this nice suggestion. I have tried to implement as you explained.

actions: [{
    name: 'Promocode Send',
    type: 'bulk',
    fields: [{
      field: 'promocodes',
      type: 'Enum',
      enums: []
    }],
    hooks: {
      load: async ({ fields, request }) => {
        console.log('>>>>>>>>>>>LOAD HOOKS>>>>>>>>>>>>');
        const promocodesField = fields.find(field => field.field === 'promocodes');
        const promocodesDetails = await promocodes.findAll({ attributes :['id', 'name']});
        console.log(">>>>>>>>>>>>>PROMOCODE DETAILS>>>>>>>>>>>>>>>>.");
        console.log(promocodesDetails);
        promocodesField.enums = promocodesDetails;
        return fields;
      }
    }
  }],

I have implemented here. It won’t console even first line.

I have tried to refresh whole page, re-run the project and all. When this hook will load.
Here is output. It seems empty.

Moreover, In the documentation provide sample code. I am confused with first line. What should I write in the my-own-helper file?

const { getEnumsFromDatabaseForThisRecord } = require('./my-own-helper');
const { getZipCodeFromCity } = require('...');
const { collection } = require('forest-express-sequelize');
const { customers } = require('../models');

collection('customers', {
  actions: [{
    name: 'Send invoice',
    type: 'single',
    fields: [
      {
        field: 'country',
        type: 'Enum',
        enums: []
      },
      {
        field: 'city',
        type: 'String',
        hook: 'onCityChange'
      },
      {
        field: 'zip code',
        type: 'String',
        hook: 'onZipCodeChange'
      },
    ],
    hooks: {
      load: async ({ fields, request }) => {
        const country = fields.find(field => field.field === 'country');
        
        const id = request.body.data.attributes.ids[0];
        const customer = await customers.findByPk(id);

        country.enums = getEnumsFromDatabaseForThisRecord(customer);

        return fields;
      },
      change: {
        onCityChange: async ({ fields, request, changedField }) => {
          const zipCode = fields.find(field => field.field === 'zip code');
          
          const id = request.body.data.attributes.ids[0];
          const customer = await customers.findByPk(id);

          zipCode.value = getZipCodeFromCity(
            customer, 
            changedField.value
          );

          return fields;
        },
        onZipCodeChange: async ({ fields, request, changedField }) => {
          const city = fields.find(field => field.field === 'city');

          const id = request.body.data.attributes.ids[0];
          const customer = await customers.findByPk(id);

          city.value = getCityFromZipCode(
            customer,
            changedField.value
          );

          return fields;
        },
      },
    },
  }],
  fields: [],
  segments: [],
});

Please suggest me. Is the way I used perfect or not?

Hello @Mayur_Malaviya,

Which version of forest-express-sequelize are you using?
The code you shew to me is compatible from the v8.

Please let me know your version.

Thanks,

@Guillaume_Cisco
“forest-express-sequelize”: “^7.0.0”

How can I upgrade it.

Alright @Mayur_Malaviya,

That’s why you won’t see any logs.
Please refer to the documentation for knowing how to upgrade to V8 with sequelize.

@Guillaume_Cisco I have read the documentation of the ForestAdmin. It seems hooks concept is available in version 7 as well.
Click here for that document.

Perfect @Mayur_Malaviya !

Yes, hooks concept is also available in version 7, but the syntax is different.
You can see the modification in the upgrade note.

I strongly recommend you to use the last version, the 8 one :smiley: as it supports bulk smart actions! It seems to be the case for you :wink:

Let me know if it helps :rocket:

@Guillaume_Cisco I need to resolve it with version 7 because I have already done lot work and syntax of action and there are lot many changing break. so, I need to find solution with version 7. As I read the document, I think bulk action with hooks is not supported in version 7.

Ok great! @Mayur_Malaviya

It looks like only single smart actions are available with hooks concepts in version 7.
Maybe transform your smart action to a single type one?

Cheers,

@Guillaume_Cisco Can you suggest any other ways?

@Mayur_Malaviya,

Maybe you could use the values concept available here, but it is deprecated and I strongly recommend you to upgrade to v8.

Be aware Values function is available for single type action only.

Let me know if it helps :wink:

@Guillaume_Cisco
I agree with you that we should upgrade version but we are near to done the whole code and move to production and during this time I can’t take risk to upgrade the version. If this project is done once, we can do it meanwhile.

Let me check that functionality. I will let you know if it helps me.

Thanks

@Guillaume_Cisco This functionality is possible with single type action only but my requirements must required bulk action type. I resolve that not using this functionality but in another way.

Thanks for your response and provide me all possible ways.

1 Like